0

I am tryig to check timestamp using php. However, the $date variable echoes out a timestamp 1382389873 which I checked with a unix converter epochconverter.com it shows Mon, 21 Oct 2013 21:11:13 GMT when it is meant to be 5 Nov 2013. Anyone can see the code below and pinpount my mistake? Thanks.

$today = strtotime(date("d.m.y"));
        $c_date = strtotime($CI->session->userdata('c_date'));
        $early = strtotime(date("d.m.y")."+2 week");
        $date = strtotime("5.11.13");
        echo'<br/>';
        echo 'test'.$date;
        echo'<br/>';
        echo 'collection'.$c_date;
        echo'<br/>';
        echo 'early'.$early;
        echo'<br/>';
        echo 'today '.$today;
        echo'<br/>';
2
  • in this example, shouldn't $date = $early? Commented Oct 22, 2013 at 2:25
  • yes but $date echoes out 138238987 which is 21 Oct as converted by epochconverter.com while $early is 1383660613 which is 5 Nov 2013. That's my problem Commented Oct 22, 2013 at 3:02

3 Answers 3

1

strtotime is very vague in the way it processes dates. It is interpreting "5.11.13" as 5:11:13pm today (Which is 21:11:13 on a 24-hour clock).

If you want to specify november 5th you should do it like so:

$date = strtotime("11/5/13");
echo $date;
echo date("m/d/Y", $date);

Output:

1383609600
11/05/2013
Sign up to request clarification or add additional context in comments.

Comments

0

Others answered this before I did, but here are my tests and notes to supplement the conversation... strtotime() converts a string to a time integer. date() converts an integer to a (optionally formatted) string. I was gonna say you can always just use that rather than epochvonverter.com... but now I understand why you went there. Also note that the manual says this:

"Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible." -http://www.php.net/manual/en/function.strtotime.php (more quirky things on that page, btw).

It is very interesting that this example with dot is NOT producing d.m.y, but h.m.s! I guess that's because we're working with strtoTIME, not... uh.. strtoDATE :)

Consider this:

$integer = 1382389873;
var_dump($integer);
echo "<br>";
//int(1382389873) 

$string = date($integer);
var_dump($string);
echo "<br>";
//string(10) "1382389873" 

$formatted=date('d.m.y',$integer);
$laterint=strtotime($formatted."+2 week");
var_dump($laterint);
echo "<br>";
// int(1383682213)

$laterstring=date('d.m.y',$laterint);
var_dump($laterstring);
echo "<br>";
// string(8) "05.11.13" 

And here's the juicy part:

$date1 = strtotime("5.11.13");
var_dump($date1);
echo "<br>";
$datestring1=date('d.m.y',$date1);
var_dump($datestring1);
    //BAD PHP, BAD!
echo "<br>";

$date2 = strtotime("5.11.2013");
var_dump($date2);
echo "<br>";
$datestring2=date('d.m.y',$date2);
var_dump($datestring2);
echo "<br>";

$date3 = strtotime("5/11/13");
var_dump($date3);
echo "<br>";
$datestring3=date('d.m.y',$date3);
var_dump($datestring3);
echo "<br>";

Really interesting stuff, guys - thank you all! The moral of the story for me is to always be explicit with 4 digit year.

Comments

0

I ran a couple of tests for you and it seems that you need to put your year in YYYY format e.g. 2013:

$date = strtotime("5.11.2013"); // your code
echo $date . "\n";
echo date('d-m-Y', $date);
// timestamp: 1383562800
// converted to date: 05-11-2013

--edit-- as I mentioned in my previous comment, if $date is supposed to be the same as $early, why are you even bothering to reassign it manually? Why not just use $early?

Comments

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.