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);
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");
How to convert this (in ISO8601 format): 2014-03-13T09:05:50.240Z
To this (in MySQL DATE format): 2014-03-13
in php?
try this
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
The complete date function documentation can be found here: http://php.net/manual/en/function.date.php
The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.
Hope I could help :)
P.s.:
Just in case strtotime will return 0 try using this:
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime(substr($date,0,10)));
Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):
$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d'); // MySQL datetime format
There is no reason to use the inefficient time functions. The most efficient way is to simply extract the first 10 characters:
substr($date,0,10)
People, that are really coding for year ≥10000, can use:
substr($date,0,strpos($date,"T"))
Simply convert datetime description into a Unix timestamp using with strtotime and then five format using Date Formats
Try it will surely work for you.
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
For those using Carbon (php library), the parse() works quite well:
Carbon::parse($date)
https://carbon.nesbot.com/docs/
Today I have published an interitty/utils package that deals with, among other things, the ISO-8601 format and perhaps all permutations of this standard.
I hope it will help you too.
$dateTimeFactory = new Interitty\Utils\DateTimeFactory();
$dateTime = $dateTimeFactory->createFromIso8601('1989-12-17T12:00:00Z');
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
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
I have a string as mentioned below:
$ts = "3/11/09 11:18:59 AM";
which I got using the date() function.
Now I need to convert this to a readable format like below
11-Mar-2009
I have tried everything using date(). How can I achieve this?
You need to convert it to something you can use for further formatting. strtotime() is a good start, which yields a unix timestamp. You can format that one using strftime() then.
strftime("%d-%b-%G", strtotime($ts));
Actually I tried doing this and it worked.
echo date("d-M-Y", strtotime($ts));
If you initially get the string from the date() function, then pass on formatting arguments to the date-function instead:
date('Y-m-d')
instead of converting the string once again.
EDIT: If you need to keep track of the actual timestamp, then store it as a timestamp:
// Store the timestamp in a variable. This is just an integer, unix timestamp (seconds since epoch)
$time = time();
// output ISO8601 (maybe insert to database? whatever)
echo date('Y-m-d H:i', $time);
// output your readable format
echo date('j-M-Y', $time);
Using strtotime() is convinient but unessecary parsing and storage of a timerepresentation is a stupid idea.
You can use the date() function to generate the required format directly, like so:
date("j-M-Y");
See www.php.net/date for all the possible formats of the output of the date() function.