Jump to content

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.<----

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.