strtotime('today') returning incorrect time?
Asked Answered
L

5

15

I am trying to create a select list starting from the current date of the user. I want it so that it is set to midnight in unix timestamp format.

This is all I'm doing:

$today = strtotime('today');
echo $today;

This is my result:

1333144800

which is: Fri, 30 Mar 2012 22:00:00 GMT according to Epoch Converter (incorrect by a couple hours.)

Linis answered 31/3, 2012 at 16:22 Comment(1)
Looks like the server's timezone is set to GMT+2, so you're actually getting the correct result.Condon
C
28

If you want strtotime() to return a timestamp relative to UTC (00:00:00 UTC instead of e.g. 00:00:00 UTC+2, if your system is set to a timezone with an offset of 2 hours against UTC/GMT), you need to specify that:

$today = strtotime('today UTC');
Condon answered 31/3, 2012 at 16:34 Comment(0)
H
6

GMT (+0) time

echo $today = strtotime('today GMT');
echo "<br>" . $today = date("d-m-Y H:i:s", $today);

We expect that your server runs at GMT - that is the best (for maneuvering with time displays later). If not, you MUST adjust php.ini set this "date.timezone = GMT".

When you get that done, you will see 00:00 with my codes.

Then, you must develop function (ie DisplayDate()) in your script to display dates of your website correctly if

  • youre not in GMT area
  • or/and if you want for your users to see times in their timezone with timezone selection for example.

DisplayDate() should include support for daylight changes also (0, or +1 hour / summer and winter time).

Hidalgo answered 31/3, 2012 at 17:12 Comment(0)
B
3
strtotime( $time )

is designed to return a unix timetamp, meaning, it will return the number of seconds since jan 1, 1970. http://www.php.net/manual/en/function.strtotime.php

To get around this, use something like:

$today = date("d/m/Y H:i:s", strtotime('today'));
echo $today;
Batchelor answered 31/3, 2012 at 16:36 Comment(0)
R
1

You might have to specifically set the time as well as the day:

$today_midnight = strtotime('today UTC 00:00');
Rescue answered 31/3, 2012 at 16:47 Comment(0)
B
0

You should check the timezone configuration in your php.ini file. In my case (I live in El Salvador) I had to change it like this:

date.timezone = America/El_Salvador
Baseboard answered 31/3, 2012 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.