[Wolves] iso8601 dates in PHP
sparkes
sparkes at westmids.biz
Mon Aug 9 18:05:59 BST 2004
Matthew Revell wrote:
>Anyone got any idea how I can convert a date of the format:
>
>2004-08-09T15:48:45+01:00
>
>into the standard Unix timestamp, using PHP?
>
>I've searched far and wide for an answer, but can't find anything I can use!
>
>
>
from http://uk.php.net/manual/en/function.strtotime.php
I am pretty sure I have done this with strtotime on it's own but this
bit of code is listed as helping
laters
sparkes
> |For versions of PHP that does not integrate that natively, here is a
> function to transform a W3C date format (subset of ISO-8601) into a
> UNIX timestamp:
>
> <?php
> function dateW3cToUnix($w3cDT)
> {//http://www.w3.org/TR/NOTE-datetime
> //1997-07-28T19:25:30.45+01:00
> if (strcasecmp('Z',substr($w3cDT,-1,1))==0)
> {
> $tzd='+00:00';
> $myDate=substr($w3cDT,0,-1);
> }
> else
> {
> $i=strrpos($w3cDT,'+');
> if ($i<1) $i=strrpos($w3cDT,'-');
> if (($i>15)&&(strlen($w3cDT)==($i+6)))
> {
> $tzd=substr($w3cDT,-6);
> $myDate=substr($w3cDT,0,-6);
> }
> else
> {
> $tzd='+00:00';
> $myDate=substr($w3cDT,0,10);
> }
> }
> $lns=strlen($myDate);
> $year=($lns>=4)?substr($myDate,0,4):1970;
> $month=($lns>=7)?substr($myDate,5,2):1;
> $day=($lns>=10)?substr($myDate,8,2):1;
> $hour=($lns>=13)?substr($myDate,11,2):0;
> $minute=($lns>=16)?substr($myDate,14,2):0;
> $second=($lns>=19)?substr($myDate,17,2):0;
> $sign=($tzd[0]=='+')?-1:1;
> $tzdHour=substr($tzd,1,2);
> $tzdMinute=substr($tzd,-2);
> $hour+=($tzdHour*$sign);
> $minute+=($tzdMinute*$sign);
> return gmmktime($hour,$minute,$second,$month,$day,$year);
> }
> ?>|
More information about the Wolves
mailing list