2

I'm having an issue trying to convert a Unix time (taken from an XML file generated by an SVN Info command) into to a formatted date/time.

The $svnInfoTime is correct, and the generated $unixTime reflects this, though the $formattedDate seems to be slightly out.

I've seen this occur in a number of (but not all) cases. An example:

<?php 
    $svnInfoTime = "2013-03-06T15:42:00.894378Z";
    $unixTime = strtotime($svnInfoTime);
    $formattedDate = date('d F, Y, h:i A', $unixTime);

    echo "SVN info time : " . $svnInfoTime . "<br>";
    echo "unix time     : " . $unixTime . "<br>";
    echo "formatted date: " . $formattedDate . "<br>";
>

outputs:

SVN info time : 2013-03-06T15:42:00.894378Z
unix time : 1362584520
formatted date: 06 March, 2013, 04:42 PM

Why is the formatted date displaying 4:42 pm, when the Unix time is 3:42 pm??

Can anyone tell me where I'm going wrong?

3
  • Learn to use DateTime objects that are timezone aware Commented Mar 7, 2013 at 10:15
  • 2
    Another workaround is to set default time zone: date_default_timezone_set('UTC');. Commented Mar 7, 2013 at 10:17
  • You forgot to define "time" adequately precisely, is the problem. Commented Mar 7, 2013 at 10:29

1 Answer 1

3

The PHP's date() function takes your timezone settings into account when outputting/formatting dates.

Check which timezone you are located in, or pass your custom timezone settings to the date function to get output as 3:42 PM.

P.S. It works fine on codepad.viper-7.com


EDIT

On passing a different timezone(Asia/Kolkata) before the date() call, results in the output 9:12 PM.

$svnInfoTime = "2013-03-06T15:42:00.894378Z";
$unixTime = strtotime($svnInfoTime);
date_default_timezone_set('Asia/Kolkata');
$formattedDate = date('d F, Y, h:i A', $unixTime);
echo "SVN info time : " . $svnInfoTime . "<br>";
echo "unix time     : " . $unixTime . "<br>";
echo "formatted date: " . $formattedDate . "<br>";

gives:

SVN info time : 2013-03-06T15:42:00.894378Z
unix time : 1362584520
formatted date: 06 March, 2013, 09:12 PM

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, fixed issue by using date_default_timezone_set('GMT'); before calling the date function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.