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

Recommended Answers

All 7 Replies

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

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 :)

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.