views:

2529

answers:

6

hey guys,

i am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy however i dont know how the date function requires a timestamp and i cant get a timestamp form this string...

anyone an idea?

thanks Matthy

+4  A: 
$timestamp = strtotime(your date variable); 
$new_date = date('d-m-Y', $timestamp);

For more:

http://php.net/manual/en/function.strtotime.php

Or even shorter:

$new_date = date('d-m-Y', strtotime(your date variable));
Kevin
+13  A: 

Use strtotime() and date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

(see strtotime and date docs on the PHP site).

richsage
What if I get this date 0000-00-00 from mySQL? it returns a wrong date like 31/12/1969 ...
Enrique
A: 

I think this is function in Php that you need. strtotime. http://php.net/manual/en/function.strtotime.php

How do u need the date in Mysql format?

A: 

You can try strftime function. Something like strftime ($time, '%d %m %Y') More here

Sinan
+1  A: 

Also another obscure possibility:

$oldDate = '2010-03-20'
$arr = explode('-', $oldDate);
$newDate = $arr[2].'-'.$arr[1].'-'.$arr[0];

i don't know would use it but still :)

Gabriel
+1  A: 
implode('-', array_reverse(explode('-', $date)));

Without date conversion overhead, not sure it'll matter much.

Tim Lytle
this is turning into a competition :D
Gabriel
Yeah, answered as you submitted yours I believe. Of course the error checking found in strtotime may be something useful.
Tim Lytle
Was useful for me in converting non-American date format...dd-mm-yyyy => yyyy-mm-ddSee http://www.php.net/manual/en/datetime.formats.date.php
Chris Jacob