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.
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 am facing the following problem when converting a date value using strtotime().
If I do: strtotime('1/1/2019') the output would be 1546293600 -> 31.12.2018 # 10:00pm (UTC)
If I do: date_default_timezone_get() the output is Europe/Bucharest
In my php.ini file (of my hosting account), the timezone is set to date.timezone="Europe/Helsinki"
I have following two questions.
Shouldn't date_default_timezone_get() output what it is configured
in my hosting accounts php.ini file? (the
date_default_timezone_set() is not being used)?
Why the strtotime() conversion result is on GMT-2 timezone? It is
my understanding that if there is no timestamp supplied, the
conversion should be done on the current time (which should be
GMT+2, either Helsinki or Bucharest)?
If I do date_default_timezone_set('UTC') the conversion result of strtotime('1/1/2019) is done correctly (on GMT+2).
Thank you.
There may be some reasons why this is happening and we can't tell you why, because we don't know your application, since it may somewhere change the time zone at runtime.
Instead you should stop using strtotime and embrace object oriented DateTime functions.
Using DateTime you can define your date along with its time zone:
// local time
$date = new \DateTimeImmutable('1/1/2019', new \DateTimeZone('Europe/Bucharest'));
echo $date->format('U > d.m.Y (e)') , '<br>';
// convert to UTC
echo $date->setTimeZone(new \DateTimeZone('UTC'))->format('U > d.m.Y (e)');
The library is quite powerful and much more verbose than operating on strings and unix timestamps.
Provide hour and timezone to the function using the full IS0 8601 format 2019-01-01T12:00:00+00:00. That way you will get the correct timestamp in UTC. If you won't use the hour I recommend to use 12 AM.
In the following example I will use a numeric date array to build this string
// [Y,m,d]
$date = [2019,1,1];
for($i=1; $i<3; $i++){
$date[$i] = str_pad($date[$i],2,'0',STR_PAD_LEFT);
}
$t1 = strtotime(implode('-',$date).'T12:00:00+00:00');
echo date('r',$t1);
//=>Tue, 01 Jan 2019 12:00:00 +0000
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
I am working with the PHP date and time functions to create my own time between dates calculator and have encountered a problem with the strtotime() function.
When working with a manually entered date and time, calculating the difference between:
20-02-1986 12:00:00 and 04-01-2014 19:31:13
returns what appears to be the correct difference, the first time converted using strtotime() and the second time retrieved using time().
However, if the one of the times is in EDT and the other is in EST, the strtotime() function seems to add an hour as if one of the times is 'falling back' to EST.
17-09-1986 12:00:00 and 04-01-2014 19:37:03
According to timeanddate.com, the difference between the two dates should be:
861,521,823 seconds
but the following code I have produces this:
861,525,423 seconds
There are an extra 3,600 seconds (1 hour). When using strtotime() on 17-09-1986 12:00:00, PHP seems to be working with 17-09-1986 11:00:00.
<?php
date_default_timezone_set('America/New_York');
$today = time();
$pastDate = '17-09-1986 12:00:00';
$pastDate = strtotime($pastDate);
$timeAlive = $today - $pastDate;
?>
You should convert both dates to GMT, so you can do the difference correctly. PHP is detecting that both times are on the same time zone, so it just does the math, disregarding the time zone change. Converting first should get rid of that problem.
php > echo strtotime("now America/New_York")."\n";
1376459035 // -> 2013-08-14 05:43:55 - Wrong
php > echo strtotime("now UTC")."\n";
1376444635 // -> 2013-08-14 01:43:55 - OK
php > echo time()."\n";
1376444635 // -> 2013-08-14 01:43:55 - OK
Can anyone explain?
Is this some PHP's invention – timezone-"corrected" unix timestamps?
// edit:
I realize it makes no sense to even specify the timezone with "now". It does, however, with other relative times, e.g. "tomorrow midnight". There, depending on the timezone, "tomorrow" could be a day further away, depending on whether the timezone is over midnight already. The behavior is equally weird, just a bit harder to explain.
As explained on Unix time wiki, Unix Epoch is always in UTC. Thats why outputs from
echo strtotime("now UTC");
echo time();
are the same. According this info New York time zone is UTC -5 hours. With current daylight saving time +1 now it equally UTC -4 hours. That's why you got result 2013-08-14 05:43:55 (4 hours diff).
you are using
strtotime("now America/New_York") thats why it is showing unix timestamp in different timezone for string.
use this:
date_default_timezone_set("America/New_York");