I have the following value:
30/05/2010 @ 09:15:15
I need to convert it to Y-m-d H:i:s.
I've tried:
$date = "30/05/2010 @ 09:15:15";
$formatteddate = date("Y-m-d H:i:s", time($date));
echo $formatteddate;
I end up with a value dated 1970. I've also tried strtotime.
Can anyone point out what I'm missing?
If you want to show date and time as per user's timezone, you can set the timezone manually using the date_default_timezone_set() function before the date() function call.
Current Date and Time <? php //current Date, i.e. 2013-08-01 echo date("Y-m-d"); //current Time in 12 hour format, i.e. 08:50:55pm echo date("h:i:sa"); //current Time in 24 hour format, i.e. 18:00:23 echo date("H:i:s"); //current Month, i.e. 08 echo date("m"); //current Month name, i.e. Aug echo date("M"); ?>
The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
The time() function does not have any parameters which is why it is going to give you an error.
I have tried to use strtotime() thinking that may work, but it is not. I will update my answer when I find something that works. However, first thing is that time() will not work.
Edit: As Phil just beat me to seconds before:
$date = str_replace("@ ", "", "30/05/2010 @ 09:15:15");
$date = str_replace("/", "-", $date);
$formatteddate = date("Y-m-d H:i:s", strtotime($date));
echo $formatteddate;
Example is here: http://codepad.org/heph1PG0
If you're using PHP 5.3, try DateTime::createFromFormat()
, eg
$dt = DateTime::createFromFormat('d/m/Y @ H:i:s', $date);
If not, strtotime()
may work but you'll need to get rid of the @
symbol and change the forward slashes to hyphens (if that's an EU / AU date), eg
$time = strtotime(str_replace(array('@', '/'), array('', '-'), $date));
Edit:
To display the dates in the format you want, use
echo $dt->format('Y-m-d H:i:s'); // for DateTime
echo date('Y-m-d H:i:s', $time); // for strtotime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With