I really like the strtotime()
function, but the user manual doesn\'t give a complete description of the supported date formats. strtotime(\'dd/mm/YYYY\')<
Simple as this
date('Y-m-d H:i:s', strtotime( str_replace('/', '-', $from_date ) ) );
I tried to convert ddmmyy format to date("Y-m-d"
format but was not working when I directly pass ddmmyy =date('dmy')
then realized it has to be in yyyy-mm-dd format so. used substring to organize
$strParam = '20'.substr($_GET['date'], 4, 2).'-'.substr($_GET['date'], 2, 2).'-'.substr($_GET['date'], 0, 2);
then passed to date("Y-m-d",strtotime($strParam));
it worked!
Are you getting this value from a database? If so, consider formatting it in the database (use date_format
in mysql, for example). If not, exploding the value may be the best bet, since strtotime just doesn't seem to appreciate dd/mm/yyyy values.
The simplest solution is this:
$date = '07/28/2010';
$newdate = date('Y-m-d', strtotime($date));
$date_info = '20/02/2019'; echo date('Y-m-d', strtotime(str_replace('/', '-', $date_info) ));
You can parse dates from a custom format (as of PHP 5.3) with DateTime::createFromFormat
$timestamp = DateTime::createFromFormat('!d/m/Y', '23/05/2010')->getTimestamp();
(Aside: The !
is used to reset non-specified values to the Unix timestamp, ie. the time will be midnight.)
If you do not want to (or cannot) use PHP 5.3, then a full list of available date/time formats which strtotime accepts is listed on the Date Formats manual page. That page more thoroughly describes the fact that m/d/Y
is inferred over d/m/Y
(but you can, as mentioned in the answers here, use d-m-Y
, d.m.Y
or d\tm\tY
).
In the past, I've also resorted to the quicky str_replace
mentioned in another answer, as well as self-parsing the date string into another format like
$subject = '23/05/2010';
$formatted = vsprintf('%3$04d/%2$02d/%1$02d', sscanf($subject,'%02d/%02d/%04d'));
$timestamp = strtotime($formatted);