I have a GMT formatted date 15/10/2012 and when I run strtotime() on it it returns false. I've tried setting the default time zone, which doesn't change anything and I've even tried doing the following.
$date = new DateTime($formatted);
$date->setTimezone('Europe/London');
return $date->getTimestamp();
Still no result however.
Anyone got any ideas please?
$date = DateTime::createFromFormat('d/m/Y', $formatted);
$date->setTimezone(new DateTimeZone('Europe/London'));
return $date->getTimestamp();
http://www.php.net/manual/en/datetime.createfromformat.php
edit: updated timezone syntax.
strtotime does not accept that format. Please see accepted formats in http://www.php.net/manual/en/datetime.formats.date.php
Related
I tried to use strtotime function to format the today's date in PHP but its giving me the wrong result. My code is given below.
<?php
$today = date("m-d-Y H:i:s");
echo date('m-d-Y H:i:s', strtotime($today));
?>
Here, I am getting this 01-01-1970 05:30:00 result.
Here, I need to get the proper datetime result.
date("m-d-Y") is what's causing issues for you. For example, take 01-02-2019 and 02-01-2019 - which is Februrary 1st and which is January 2nd? That format will make strtotime() return false, as it doesn't know what format that is for days that are greater than 12.
d-m-Y would be expected and a valid format.
You can use DateTime::createFromFormat() instead. Then you can create a valid DateTime object from that format, and use it however you need it to.
$today = DateTime::createFromFormat("m-d-Y H:i:s", date("m-d-Y H:i:s"));
echo $today->format("m-d-Y H:i:s");
Live demo
Documentation for DateTime::createFromFormat()
Alternatively, if you just need to print the date directly and not process it further, you don't need to go through any hoops and can just use date() as you were, without the second line. But you can not use that result in a strtotime() function, as it will return incorrect results.
echo date("m-d-Y H:i:s");
So, if I have a string like so: '2017-12-01T16:03:00' and need to convert this string into the timezone of America/New_York, how to convert this string? Not exactly sure what the T is doing in the string and what it's for exactly.
I'm curious on the correct method for this, while I believe the T here is important, I understand the purpose of it. What I've tried here so far:
$string = '2017-12-01T16:03:00';
$time_fix = explode('T', $string);
if (count($time_fix) > 1)
{
$data_date = DateTime::createFromFormat('Y-m-d H:i:s', $time_fix[0] . ' ' . $time_fix[1]);
$output = $data_date->format('Y-m-d H:i:s', new DateTimeZone('America/New_York'));
} else {
$data_date = DateTime::createFromFormat('Y-m-d H:i:s', $time_fix[0] . ' 00:00:00');
$output = $data_date->format('Y-m-d H:i:s', new DateTimeZone('America/New_York'));
}
Not really sure if this is converting the timezone or not, so wondering on what your thoughts are on this? Will the $output variable contain the date and time for the America/New_York timezone? It is very difficult to test this as I don't know what timezone it is currently in when the time string gets created.
Also, wondering if the T here is important and what it means. And how to do this properly for the America/New_York timezone?
UPDATE
Apparently, I found out that the Server Timezone that is creating these Date strings via the API is in Eastern Standard Time already. But if I didn't know this, how would it be converted to UTC? The developers of the API, states that it is UTC in 8601 format, but the timezone is EST. This does not make sense to me. Because, I believe EST is UTC-5, not just UTC. How the heck am I supposed to know this from what seems to be an improperly handled UTC timezone string result from the API? Am I correct in stating that 2017-12-01T16:03:00 is not the correct string for EST timezone from a UTC timezone?
If you do:
$date = new DateTime($info['CreateDate']);
$date->setTimeZone(new DateTimeZone('UTC'));
$date->setTimeZone(new DateTimeZone('America/New_York'));
$output = $date->format('Y-m-d H:i:s');
You are first creating the DateTime object in your default timezone which will affect the timestamp calculation, and then changing the timezone.
Instead, assuming UTC input, you should do:
$date = new DateTime($info['CreateDate'], new DateTimeZone('UTC'));
$date->setTimeZone(new DateTimeZone('America/New_York'));
$output = $date->format('Y-m-d H:i:s');
This way the DateTime object will interpret the input date as UTC time and correctly compute the timestamp. Then setting the timezone is just to get the right date string, it does not affect the underlying timestamp.
I notice that ISO 8601 date strings can specify UTC offsets so in this particular case you may get away without specifying it as no offset is given. This will also make no difference in this case if your default timezone is UTC.
Edit based on new data:
If you know the input is in Eastern Standard Timezone try changing the line where you construct the DateTime object.
$date = new DateTime($info['CreateDate'], new DateTimeZone('EST'));
$date->setTimeZone(new DateTimeZone('America/New_York'));
$output = $date->format('Y-m-d H:i:s');
Explode separate your $string the elements on 2017-12-01 and 16:03:45.
And here you have the answer Timezone conversion in php
If I have the following code:
var_dump(strtotime('2:28:15am 28/11/2013'));
It returns false. What is causing this?
That format is probably not a format that strtotime() can interpret. Try using DateTime::createFromFormat() instead:
$dt = DateTime::createFromFormat('g:i:sa d/j/Y', '2:28:15am 28/11/2013');
echo $dt->format('Y-m-d H:i:s');
I had to guess at the exact formatting of your dates. But you can easily edit that by using the appropriate formatting options listed here.
There is a problem with your time format, strtotime works with the following format for sure:
$date->format('Y-m-d G:i:s');
strtotime($date);
The server's locale is set correctly, on other domains it seems to be fine... however I am currently experiencing a strange problem.
If I do a straight strtotime('now'); it returns the right timestamp (for today's date/time in my timezone) however if I do:
strtotime('07/09/2012 13:48');
It returns a timestamp that's for 9th July, like it's reading the date as a US format. I've retrieved the timezone and it is set to Europe/London (by using date_default_timezone_get).
Any ideas?
$date = DateTime::createFromFormat('d/m/Y H:i', '07/09/2012 13:48');
echo $date->format('Y-m-d H:i');
http://www.php.net/manual/en/datetime.createfromformat.php
Use the DateTime class and DateTime::createFromFormat() method.
strtotime reads it as the US format. Best to use the ISO yyyy-mm-dd.
See this for valid date formats
pls i want to convert this 06-29-2010 04:00PM to this format Y-m-d h:i:s in php
thanks
Use strtotime:
$date = '06-29-2010 04:00PM';
$date = str_replace('-', '/', $date);
echo date('Y-m-d h:i:s', strtotime($date));
Result:
2010-06-29 04:00:00
I'll go ahead and offer PHP's date() manual as a second resource.
If you need to convert one timestamp or check your calculations you can use a web tool like www.unixstamp.com (just keep in mind it uses GMT)