Hello Lili
Hello Lili

Reputation: 1587

Strtotime outputs wrong date

Given the following string: "03-02-2015" (d-m-Y format), when I apply

strtotime("03-02-2015");

it gives me 1422914400.

When I transform it back into date again, just to verify, using more unix timestamp convertors on the internet, they prompt: 02-02-2015.

Why does strtotime convert the string into a timestamp which is actually yesterday? How can I fix this?

For strtotime("now") it outputs the correct results when verified with convertors on the internet.

Upvotes: 0

Views: 1490

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

using more unix timestamp convertors on the internet

Forget them, your language PHP itself provides you ways to handle that, use them

echo date("Y-m-d",strtotime("03-02-2015"));   

Prints

2015-02-03

There is nothing wrong with that strtotime output.

@SunilPachlangia I want strtotime to generate the correct timestamp (the one corresponding to today, not yesterday)

Then simply use

$timestamp=time();

Now for the real reason why you see different dates. One single Unix timestamp will show different time on different time zones, since time zones vary quiet a bit you can see one day difference if the place where you generated the timestamp uses a different time zone compared to where you are using that timestamp.

Here is a practical demonstration for you

<?php
$ts=strtotime("03-02-2015");                        //2015-02-03
date_default_timezone_set("America/Chicago");
echo date("Y-m-d",$ts);                             //2015-02-02 :)

Fiddle

Upvotes: 1

Related Questions