0

Hi I currently have date in database in this format:

11-7-2009

But I think I need to echo this in this format: 2009-11-07

Edited by codewalkz: n/a

5
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by emhmk1
1

There's probably a better way to do this, but this works:

<?php
$date = "11-7-2009";     // Date from database in MM-DD-YYY format
$date = explode("-", $date);     // Split date into array of numbers
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]);     // Convert date into timestamp
$modified_date = date('Y-m-d', $time);     // Convert timestamp into YYYY-MM-DD format
?>
0

Alternative to Lsmjudoka's solution:

<?php
$a = '11-07-2009';
$b = explode('-', $a);
echo $b[2] .'-'. $b[0] .'-'. $b[1];
?>

Maybe I'm wrong but, in this case, I don't see any advantage in using mktime()
bye :)

0

This just did what I needed to do. Kudos!

<?php
$date = "11-7-2009";     // Date from database in MM-DD-YYY format
$date = explode("-", $date);     // Split date into array of numbers
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]);     // Convert date into timestamp
$modified_date = date('Y-m-d', $time);     // Convert timestamp into YYYY-MM-DD format
?>

Edited by codewalkz: n/a

0

Or you could simply call this function

<?php

$date = '01/01/2003'; // OR CALL FROM DATABASE $date = $row['date'];
  function changeDate($date)
   { 
   $newdate = date('Y/m/d',strtotime($date));
   return $newdate;
   }

// TO CALL THE FUNCTION
echo changeDate($date);


?>

Hope it helps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.