proctk Posted March 6, 2008 Share Posted March 6, 2008 Hi I have a date value assigned to a variable $date which is in this format 2008-03-05. How would I use php to reorganize the date so that is reads Wednesday, March 05 2008 Thank you Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/ Share on other sites More sharing options...
uniflare Posted March 6, 2008 Share Posted March 6, 2008 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 More sharing options...
proctk Posted March 7, 2008 Author Share Posted March 7, 2008 thank you for the post however I think there is a small glitch in the code 2008-03-11 returns March 03 2008 any idea why Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485634 Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 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 More sharing options...
kenrbnsn Posted March 7, 2008 Share Posted March 7, 2008 Use: <?php $date = '2008-03-05'; echo date('l, F j Y',$date); ?> Ken Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485708 Share on other sites More sharing options...
proctk Posted March 7, 2008 Author Share Posted March 7, 2008 I tested this code and it does not work it returns Wednesday, December 31 1969 Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485724 Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 There needs to be a strtotime() function in there. Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485726 Share on other sites More sharing options...
proctk Posted March 7, 2008 Author Share Posted March 7, 2008 how would I work that in Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485738 Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 <?php $date = '2008-03-05'; $date = strtotime($date) echo date('l, F j Y',$date); ?> my guess Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485745 Share on other sites More sharing options...
proctk Posted March 7, 2008 Author Share Posted March 7, 2008 The last post worked, thank you for all the help Link to comment https://forums.phpfreaks.com/topic/94635-reformat-date/#findComment-485750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.