How can I convert this:
Thursday, 14 August 2014
to this in php:
2014-08-14
I tried strtotime(), trim(), etc.. and realised thats not it. Can you guide me on this one please?
How can I convert this:
Thursday, 14 August 2014
to this in php:
2014-08-14
I tried strtotime(), trim(), etc.. and realised thats not it. Can you guide me on this one please?
It works just fine for me with strtotime.
$time = strtotime("Thursday, 14 August 2014");
echo date("Y-m-d",$time);
if you're facing the problem of converting a human readable date to numeric values you have to reconsider your design. Your code should work only with 32 or 64 bit epoch values (seconds since 1st January 1970) and then convert them into the appropriate format only when needed.
In any case, just do this:
$time = strtotime(stripslashes("Thursday, 14 August 2014"));
echo date("Y-m-d",$time);
remember to strip out slashes and commas because they can confuse strtotime
I would avoid such a solution because it's NOT portable. You'll find PHP versions in which such solution doesn't work, moreover slightly different input strings could make strtotime to fail and most of all -> locale matters!!!! (i.e. what if the string is in italian language???)
Just work with epoch values and make a conversion only to show them to the end user! :-)
First convert Convert the string to time using strtotime()
and than use date()
function in php to do this. this function formats a local date and time, and returns the formatted date string.
Your code should look like this
<?php
$orignaltime = strtotime("Thursday, 14 August 2014");
echo date("Y-m-d",$orignaltime);