My variable $createdDate has a value '2014-06-23T01:44:22Z' something like this in it. It's fetched from database.
And when I convert it into standard format using strtotime(), the time returned is always 2 hours different(ahead) from the orginal variable's time.
$time1 = strtotime($createdDate);
$cretime_formated=date("Y-m-d h:i:s",$time1);
Output of $cretime_formated is 2014-06-23 03:44:22 instead of 2014-06-23 01:44:22. Why is there a difference of 2 hours in the time??? Is there anything wrong with my conversion process??
Given that the date is in Zulu timezone (and assuming all such timestamps are in that same timezone), you would need gmdate() to format it:
$cretime_formated = gmdate("Y-m-d h:i:s",$time1);
I think you have the time difference .You need to set it in UTC
date_default_timezone_set('UTC');
See Complete Refrence
List of Supported Timezones
Related
I have a xml file, containing several dates, in this format: 2016-07-23T07:00:00.000Z. I'm using a php function to convert this in to a format for publishing on a website. This should actually result in something like Saturday, 24th of July (24th, not 23rd, because of the time offset. My function somehow ignores the T07:00:00.000Z part and thus returns Friday, 23rd of July. Can anybody help me out with the proper way to convert this date?
Thanks, Peter
The string in question
2016-07-23T07:00:00.000Z
is a W3C datetime format (W3C DTF) (Complete date plus hours, minutes, seconds and a decimal fraction of a second) which can be properly parsed incl. the fractions of a second with the date_create_from_format](http://php.net/date_create_from_format) function:
$originalDate = "2016-07-23T07:00:00.000Z";
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
It does create a new DateTime which then can be formatted with the for PHP standard codes, e.g.
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->format('Y-m-d H:i:s'); # 2016-07-23 07:00:00
As that W3C format carries the timezone already and it is UTC, and you wrote you want a different one, you need to specify it:
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->setTimezone(new DateTimeZone('Asia/Tokyo'))
->format('Y-m-d H:i:s');
The reason why this is not visible (and controlable with the code given) in the previous answer is because date formats according to the default set timezone in PHP where as each DateTime has it's individual timezone.
An equivalent with correct parsing (incl. decimal fraction of a second) with the other answers then is:
$dateTime = date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
date('Y-m-d H:i:s', $dateTime->getTimestamp());
Hope this explains it a bit better in case you need the complete date value and / or more control on the timezone.
For the format, see as well: In what format is this date string?
$oldDateTime= "2016-07-23T07:00:00.000Z"; // Your datetime as string, add as variable or whatever.
$newDateTime= date("Y-m-d H:i:s", strtotime($originalDate));
Let's suppose we have this date 1972-12-31 23:59:59, if we get the TimeStamp for it from DateTimeImmutable object we will get this:
$formattedDate = '1972-12-31 23:59:59';
$ts = (new DateTimeImmutable($formatedDate))->getTimestamp(); // <- 94690799
The problem that if you try to revers the conversion, so it becomes from timestamp into formatted date:
$ts = 94690799;
$formattedDate =
(new DateTimeImmutable(sprintf('#%s', $ts)))->format('Y-m-d H:i:s'); // <- 1972-12-31 22:59:59
There is an hour gone in the second way.
So the million dolor question would be, which one of those timing is corresponding to the correct time?
Is this a bug? Or am I messing something in here?
When you create a DateTime object from a formatted string, it is created in your server's default timezone (see date_default_timezone_get). But Unix timestamps don't have a timezone - they're always in UTC. So if you write:
(new DateTimeImmutable('1972-12-31 23:59:59'))->getTimestamp();
then what you're really asking PHP is "How many seconds after 1970 in UTC was it, when it was that date + time in my current timezone". In your case, the server looks like it is running one hour ahead of UTC, hence the difference.
Crucially, when you do the inverse and create a DateTime object from a timestamp, the object's timezone is always set to UTC. There's a brief note about it on this manual page.
If you set the default timezone to UTC before running the code, you'll see that the output matches. I've added an example here: https://3v4l.org/2Rfp3
I have this UTC Date 2017-07-16 12:00:07.8 UTC (that calls $dateconvert) and I would like to convert it into seconds.
I tried to use strtotime() but it returns seconds in UTC removing another two hours and I don't understand why.
I'm in Italy and here Date is UTC+2, maybe strtotime() read $dateconvert with Italy time zone and when convert it into seconds removing that 2 hours?
Is it possible?
$anno_emsc= 2017;
$mese_emsc= 7;
$giorno_emsc= 16;
$ora_emsc= 12;
$minuto_emsc= 0;
$secondo_emsc= 7;
$dataconvert= strtotime($anno_emsc.'-'.$mese_emsc.'-'.$giorno_emsc.' '.$ora_emsc.':'.$minuto_emsc.':'.$secondo_emsc)+0;
OUTPUT
1500199207 // strtotime removed 2 hours
Since you already know all the parts, you can use gmmktime() to convert it into a timestamp. The "gm" version of mktime() knows that it's receiving a GMT/UTC date and will process it accordingly.
Try and set your date_default_timezone_set() to your correct timezone and see if that fixes your issue. Sometimes your php instance will default to the wrong timezone.
I have a xml file, containing several dates, in this format: 2016-07-23T07:00:00.000Z. I'm using a php function to convert this in to a format for publishing on a website. This should actually result in something like Saturday, 24th of July (24th, not 23rd, because of the time offset. My function somehow ignores the T07:00:00.000Z part and thus returns Friday, 23rd of July. Can anybody help me out with the proper way to convert this date?
Thanks, Peter
The string in question
2016-07-23T07:00:00.000Z
is a W3C datetime format (W3C DTF) (Complete date plus hours, minutes, seconds and a decimal fraction of a second) which can be properly parsed incl. the fractions of a second with the date_create_from_format](http://php.net/date_create_from_format) function:
$originalDate = "2016-07-23T07:00:00.000Z";
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
It does create a new DateTime which then can be formatted with the for PHP standard codes, e.g.
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->format('Y-m-d H:i:s'); # 2016-07-23 07:00:00
As that W3C format carries the timezone already and it is UTC, and you wrote you want a different one, you need to specify it:
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->setTimezone(new DateTimeZone('Asia/Tokyo'))
->format('Y-m-d H:i:s');
The reason why this is not visible (and controlable with the code given) in the previous answer is because date formats according to the default set timezone in PHP where as each DateTime has it's individual timezone.
An equivalent with correct parsing (incl. decimal fraction of a second) with the other answers then is:
$dateTime = date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
date('Y-m-d H:i:s', $dateTime->getTimestamp());
Hope this explains it a bit better in case you need the complete date value and / or more control on the timezone.
For the format, see as well: In what format is this date string?
$oldDateTime= "2016-07-23T07:00:00.000Z"; // Your datetime as string, add as variable or whatever.
$newDateTime= date("Y-m-d H:i:s", strtotime($originalDate));
I need to convert a specific date format into local time (Europe/Sarajevo), I have the time in this format 2013-02-17T15:00:00Z which I don't really understand and this is why I don't know how to convert it to the Europe/Sarajevo time, who knows maybe it is already Sarajevo time, I don't know...
OK I can parse it and remove the T and Z and get a time but these letters mean something, probably they affect the result time...
The result time is for example 2013-02-17 18:00:00, probably there will be a difference due to the letters T and Z which are probably time offset.
Use DateTime. It's much better for working with timezones:
$datetime = new DateTime('2013-02-17T15:00:00Z');
$datetime->setTimeZone(new DateTimeZone('Europe/Sarajevo'));
echo $datetime->format('c');
Reference
DateTime
DateTimeZone
See it in action
You can use php date function like this
$date = '2013-02-17T15:00:00Z';
echo date('Y-m-d H:i:s',strtotime($date));
See the Manual