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.
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 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
I am working on a project and I am having an issue formatting an epoch time to a human readable time.
I have the following epoch time 1428512160 and when I put this through epochconverter.com I get the human time of 08/04/2015 17:56:00 GMT+1:00 DST as expected.
I then use the following code in order to perform the conversion from the epoch time to a human date time.
$dt = new DateTime($supportDetails["Reported"]);
$reportedTimeString = $dt->format('d-m-Y H:i:s');
$supportDetails[Reported] is the epoch time (I've printed it so I know it's correct).
The result I get back however is 08-04-2160 14:28:51.
You need to add an # for the timestamp in the DateTime class, like this:
$dt = new DateTime("#" . $supportDetails["Reported"]);
//^ See here
You can also see this in the manual. And a quote from there:
Unix Timestamp "#" "-"? [0-9]+ "#1215282385"
Also note that the current timezone is getting ignored, which you can also see in the manual:
Note:
The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. #946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
Printing date and time is correct.
Its based on what GMT you have set in your PHP.
If you printing with GMT you will get required result.
Try the following code:
$reportedTimeString = date("d-m-Y H:i:s", $supportDetails["Reported"]);
Or the following:
$date = new DateTime();
$date->setTimestamp($supportDetails["Reported"]);
$reportedTimeString = $date->format("d-m-Y H:i:s");
The problem I see is with your formatting.
If you look at PHP's date function you can see that you just need to write each portion of the desired date & time into a string.
The following formatting gives the same output you were looking for:
$dt = new DateTime($supportDetails["Reported"]);
$reportedTimeString = $dt->format('d/m/Y H:i:s \G\M\TP T');
I'm aware of two ways to get a timestamp from a relative date/time string in PHP:
strtotime: Allows user to specify their own $now value
DateTime object: Allows user to specify their own $timezone value
Is there a way to get a timestamp from a date/time string that allows one to specify both the timezone AND the $now value? It seems like the only way would be to use strtotime while temporarily overriding the default timezone for the entire php application and then setting it back immediately afterwards. That just seems like a hacky solution, and it would be nice if there were a cleaner way.
Edit: there seems to be some misunderstanding about what I'm trying to do. Here's a more concrete example:
"I want to find the timestamp corresponding to the string 'next tuesday at 3:00pm' within the America/Los_Angeles timezone AND specifying an arbitrary value for $now, such as March 14th, 2014 at 8:05am."
I've prepared an example. This may be want you want:
<?php
// Including the timezone int the time strings (thanks #Mike B!!!)
// will make it very easy. just strtotime() is required
// create a timestamp for March 14th PDT
$now = strtotime('14 March 2014 8:05am America/Los_Angeles');
// get the requested timestamp.
$nexTuesday = strtotime('next tuesday 3:00 pm America/Los_Angeles', $now);