Jump to content

Reformat date


proctk

Recommended Posts

try

 

<?php

// Date
$touchoftechDate = "2008-03-05";

// Split String into an Array
$array = explode("-",$touchoftechDate);

// Implode the array into a String in the correct order
$touchoftechDate = implode("-",array($array[2],$array[1],$array[0]));

// strtotime will convert the string to a timestamp, date() will convert the timestamp to a formmated date.
$date = date("l, F m Y",strtotime($touchoftechDate));

// echo the result
echo($date);

?>

 

Dates MUST be in format: YYYY-MM-DD

 

you could also wrap this into a function

 

 

hope this helps,

Link to comment
https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-484579
Share on other sites

Because the "m" in the date() function format string means month. "d" or "j" would be needed to give the day number. Using (or debugging) any code involves making use of the programming language manual. uniflare just gave a quick example of how you could do this and it was likely untested, like most of the quick code examples that are posted in a forum. When it did not work, you could have used the manual to find out why and make it work yourself.

 

In fact all the code to explode and implode the date is unnecessary because strtotime() works on a yyyy-mm-dd format.

Link to comment
https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485693
Share on other sites

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.