I do
strtotime("2008-09-04")
Where 09 is the month and 04 is the day and I get the result:
1220500800
Which is Thu, 04 Sep 2008 04:00:00 GMT. Where does those 4 hours come from? I should get 1220486400 instead of 1220500800 from strtotime.
You can set timezone globally with function date_default_timezone_set('UTC');, or you can just set timezone locally when you call strtotime() function like:
echo strtotime("2008-09-04 +0000"); # 1220486400
As people above have said, you're likely suffering from a time zone mismatch. This function may be of use in debugging the issue: http://us3.php.net/date_default_timezone_get
The most common problems with PHP's strtotime are timezones and date-time formats. I will address those 2 points.
First the format. As suggested by others on stackoverflow use the iso 8601 date format YYYY-MM-DD (https://www.iso.org/iso-8601-date-and-time-format.html). For example, September 27, 2012 is represented as 2012-09-27.
Next the timeszones. The best of all , do not use any timezone use the Universal Coordinated Time UTC (which is universal time and corresponds with GMT without Daylight Saving). UTC is the time standard commonly used across the world. The world's timing centers have agreed to keep their time scales closely synchronized - or coordinated - therefore the name Coordinated Universal Time (https://www.timeanddate.com/time/aboututc.html).
So now we have the picture clear. To convert a date time into unixtimestamp do:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");
// Converts a date-time into a unix timestamp (= 1560470400).
$unixTS = strtotime( "2019-06-14 00:00:00 UTC");
// Restore the timezone
date_default_timezone_set($saveDDTZ");
To restore a unix timestamp to a Date use:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");|
$unixTS = 1560470400
$dateTime = date("Y-m-d H:i:s", $unixTS);
// Restore the timezone
date_default_timezone_set($saveDDTZ");
If you want to change the UTC date into a TimeZone use the difference of the UTC and the TimeZone and the Daylight Saving.
Related
When I try to turn a german date (date of birth, to be more specific) format into a unix timestamp, I am getting -3600 instead of a 0.
Maybe some summertime thing?
$value = '01.01.1970';
$date = DateTime::createFromFormat ('d.m.Y', $value);
$date->setTime(0, 0);
$value = $date->getTimestamp();
echo $value; // -3600
I always thought there's a 0. What is the best practice when dealing with a situation like this? Timezone is GMT+1, in case it matters. I even tried 01.01.1850, turning it to timestamp and then back to a formated date. Works nicely at home, but at work it showed 31st December 1849.
I suspect, if you check with date_default_timezone_get, that your server's timezone is not set to UTC. I was able to duplicate this behavior like so:
date_default_timezone_set('Europe/Berlin');
$value = '01.01.1970';
$date = DateTime::createFromFormat ('d.m.Y', $value);
$date->setTime(0, 0);
$value = $date->getTimestamp();
Due to the timezone differences, 1970-01-01 00:00:00 in the Berlin timezone does, in fact, correspond to a negative Unix timestamp of -3600. The Unix timestamp 0 just corresponds to 1970-01-01 00:00:00 in UTC - a negative value simply indicates a time prior to that instant.
That is a great question. One which even threw me off, because of this.
However, in your case Germany was as you said on UTC+01:00, hence on your system the value of $date is 1970-01-01T00:00:00+01:00.
If you reverse the scenario and try to find out what is the date corresponding to timestamp 0, it should help you understand how time zones work with timestamps.
$date = DateTime::createFromFormat ('U', '0');
$date->setTimezone(new DateTimeZone('Europe/Berlin'));
echo $date->format(DATE_ATOM); // 1970-01-01T01:00:00+01:00
Because timestamps are the relative time difference from the UNIX epoch in seconds it will be a different time in different time zone. This count starts at midnight on January 1st, 1970 at UTC. While both Britain and Germany were on UTC+01:00 in 1970, the UNIX epoch started for them on 1970-01-01 01:00:00
In terms of how to best deal with this, well... that is opinion-based. Probably most people would tell you to deal with time in UTC so you don't run into weird issues such as this, and only convert to the right timezone on display, but even the great Jon Skeet had different opinion.
I have a string $StartDate = "2015-09-23" (should be like yyyy-mm-dd).
Than I make $UdtStart= strtotime($StartDate) that returns 1442980800;
Well if I go to this link it return back "Wed, 23 Sep 2015 04:00:00 +0000".
First, why do we have 04:00:00 added?
Than, if I do this $back=gmdate("Y-m-d H:i:s", ($UdtStart)); I will have "2015-09-26 04:00:00".
What am I missing?
$UdtStart= strtotime($StartDate);
$back=gmdate("Y-m-d H:i:s", ($UdtStart));
Wed, 23 Sep 2015 04:00:00 +0000
Note that +0000 on the end, that means the time is UTC. As per the PHP strtotime() doco:
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
The gmdate is for Greenwich Mean Time (and really should be called something like utcdate nowadays), so you're asking for the data in a different foramt from what you gave it.
I'd be willing to bet money that you're in a timezone four hours removed from UTC, which is why you're seeing that.
If you want local time, use date() rather than gmdate(). The gmdate doco states:
Identical to the date() function except that the time returned is Greenwich Mean Time (GMT).
I am trying to convert a date to a timestamp at the exact midnight point.
To do this, I am using the following little function.
function converttotimestamp($date)
{
$date = str_replace('/', '-', $date);
$date = $date.' 00:00:00';
$date = DateTime::createFromFormat('m-d-Y H:i:s',$date);
return $date->getTimestamp();
}
So as you can see, I am attaching a midnight time at the end.
I tried using this as shown below
echo converttotimestamp('7/22/2014');
So as you would expect when you run this in a unix converter, you would get 1405987200.
But In my case it returns 1405976400 whicj translates to Mon, 21 Jul 2014 21:00:00.
Oh. I am in Kenya.
The reason you may be seeing a different time returned than the one you were expecting is likely because you haven't considered the relevant timezones. There are a couple different ways you can set the timezone. You can set it during runtime:
http://php.net/manual/en/function.date-default-timezone-set.php
You can set it in your PHP config file:
http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
Or you can set the timezone of your DateTime object:
http://php.net/manual/en/datetime.settimezone.php
Whenever you are converting between dates, you must consider the relevant timezone, as this is the only way for the system to determine how to switch between date formats, make comparisions and output specific dates and times. For example, if you want to convert a date and time to a timestamp, the system must know the timezone of the input date and time so it can convert properly. Take a look at strtotime:
http://us2.php.net/manual/en/function.strtotime.php
Unix timestamps are GMT timezone, so make sure you convert your datetimes accordingly. HTH.
In PHP 5.2, I'm using the following code to get a timestamp from a DateTime object.
$dateTime = new DateTime("now", new DateTimeZone("America/Los_Angeles") );
echo $dateTime->format("U");
the problem is that format("U") simply returns server timestamp, which is UTC.
How do I make it to return a timestamp from Pacific Time Zone (Los Angeles) ?
Your concept for timestamp is not right, timestamp is timezone independent, it is defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970.
Try setting timezone at top of PHP script. I think timestamps are always UTC. Use date() function to format it into what you need.
// set timezone to pacific time
date_default_timezone_set('America/Los_Angeles');
I need to convert a given GMT date and time (YYYY-MM-DD HH:MM) into such a string YYYYMMDD representing Eastern coast date. Do you think the code below is OK?
$date='2011-11-07 04:30';
$date.='-4 hours';
$date=strftime('%Y-%m-%d %H:%M',strtotime($date));
$y=gmdate('Y');
$date2=date('Y-m-d 02:00',strtotime($y.'-03-01 second sunday'));
$date3=date('Y-m-d 02:00',strtotime($y.'-11-01 first sunday'));
if($date<=$date2||$date>=$date3) {
$date.='-1 hour';
$date=strftime('%Y-%m-%d %H:%M',strtotime($date));
}
$date=date('Ymd', strtotime($date));
I'd advise against trying to do the calculation yourself. There's too many nuances with our wonderful, ever-changing, time standard. Instead, rely on PHP to perform the calculations by using php.net/date_default_timezone_set to set your timezone to GMT, then strtotime() (or the DateTime class) to get the unix-timestamp value.
Once you have the unix timestamp, use php.net/date_default_timezone_set again to set the timezone to America/New_York and use date()