Jump to content

Date from Y-m-d to m-d-Y


TRI0N

Recommended Posts

I'm having a serious brain fart here.  Use to know how to do this with explode.

 

MySQL Date Field in format of "Y-m-d" and want to convert that to "m-d-Y" for display in a php page.

 

Something like explode("-",$d_date) or something like that.. Or it had %'s in it to seperate each portion in 3 parts.  I'm pulling my hair out trying to remember how it's done.

 

Best regards,

 

TRI0N

Link to comment
https://forums.phpfreaks.com/topic/51486-date-from-y-m-d-to-m-d-y/
Share on other sites

DATE_FORMAT() is a darn site easier, but FYI using the CURDATE() function, you could use the following...

 

$sql = "SELECT CURDATE()";

$query = mysql_query($sql, $conn);

$row = mysql_fetch_array($query);

 

$date = explode('-', $row[0]);

$date_y = $date[0];

$date_m = $date[1];

$date_d = $date[2];

 

$array = array("$date_m", "$date_d", "$date_y");

$new_date = implode("-", $array);

 

echo $new_date;

This is a stored date in an existing database.  Its in the Y-m-d format.

 

Now since I'm pulling out more then just the date using a query but don't want to do a whole new query just for date.

 

In terms will this work?

 

$d_date = $row[21] ;

$date = explode('-', $d_date) ;

$date_y = $date[0] ;

$date_m = $date[1] ;

$date_d = $date[2] ;

 

$disp_date = $date_m."/".$date_d."/".$date_y ;

 

I would think this would work...

Hi.

I guess that would be easy if you use a function to change your date to the new format.. Maybe this helps

 

<?php


function change_date_format($date){

       list($year,$month,$day) = explode("-",$date);
       $newdate = $month."-".$day."-".$year;
       return $newdate;
}

//  So.. if 
$row[0]="2007-05-15";

echo change_date_format($row[0]);
//prints 15-05-2007;

?>

 

Hope this helps.. 8)

---> I.R.V.<----

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.