shahzad Posted January 26, 2010 Share Posted January 26, 2010 I am storing date as 2010-01-26 in Mysql But i want to display it as November 29th, 2009 to the user how to format the date?? thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/ Share on other sites More sharing options...
jl5501 Posted January 26, 2010 Share Posted January 26, 2010 There are many functions for formatting dates, and everybody has their preferences. My favourite is strftime() as I am an old C programmer. That takes a unix timestamp and formats it how you like, so first you have to convert your date to a unix timestamp with strtotime() so in your case, this should give you what you want assuming your original date is in $mydate then $formatted date = strftime("%B %d, %Y",strtotime($mydate)) that only is missing the st, nd, rd, th bit on the day, which you can add some other code to produce I will provide some code I wrote for that if you like Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001719 Share on other sites More sharing options...
oni-kun Posted January 26, 2010 Share Posted January 26, 2010 I am storing date as 2010-01-26 in Mysql But i want to display it as November 29th, 2009 to the user how to format the date?? thanks in advance A correct example would be this: $date = "2010-01-26"; $date_new = strtotime($date); //Returns: January 26th, 2010 echo date('F jS, Y', $date_new); Or simply this: $date = "2010-01-26"; echo date('F jS, Y', strtotime($date)); Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001723 Share on other sites More sharing options...
jl5501 Posted January 26, 2010 Share Posted January 26, 2010 Yes, date() does seem to have a few extra formatters over strftime(), but you tend to stick with one method, when you have been using it for over 30 years Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001728 Share on other sites More sharing options...
oni-kun Posted January 26, 2010 Share Posted January 26, 2010 Yes, date() does seem to have a few extra formatters over strftime(), but you tend to stick with one method, when you have been using it for over 30 years C++ (what PHP is built on) has only been out for 27 years Just kidding, good to see use of the good old functions here or there. Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001729 Share on other sites More sharing options...
shahzad Posted January 26, 2010 Author Share Posted January 26, 2010 thanks both of you... its working fine jl5501 yes provide me the code i want to see how to use strftime and have st, nd, rd, th bit.. Thanks Again Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001737 Share on other sites More sharing options...
salathe Posted January 26, 2010 Share Posted January 26, 2010 C++ (what PHP is built on) ... Oh really? There goes my learning C to contribute to the source. Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001738 Share on other sites More sharing options...
oni-kun Posted January 26, 2010 Share Posted January 26, 2010 thanks both of you... its working fine jl5501 yes provide me the code i want to see how to use strftime and have st, nd, rd, th bit.. Thanks Again http://php.net/manual/en/function.strftime.php The code will be a mashup of IF statements. Strftime does not include any code to calculate st, nd, rd, th. It's more efficient and the offical function to use date, strftime was meant to format locales not calculate a number suffix. EDIT: Salathe, no wonder why there are so many bugs ! JK!! Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001740 Share on other sites More sharing options...
shahzad Posted January 26, 2010 Author Share Posted January 26, 2010 thanks yes to use date is efficient only 2 line code and im done... Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001742 Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2010 Share Posted January 26, 2010 Or you could directly retrieve the value in your query the way you want it - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format Zero lines of slow parsed, tokenized, interpreted php code are needed and the slow conversion into and out of a Unix timestamp can be avoided as well. Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001743 Share on other sites More sharing options...
jl5501 Posted January 26, 2010 Share Posted January 26, 2010 I take the point about date() vs strftime() but I did do the suffix thing for strftime and this is what I used $day = strftime("%d",strtotime($orig_date)); if($day == 1 || $day == 21 || $day == 31) $dext = 'st'; else if($day == 2 || $day == 22) $dext = 'nd'; else if($day == 3 || $day == 23) $dext = 'rd'; else $dext = 'th'; $new_date = strftime("%B %e$dext %Y",strtotime($orig_date)); Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001744 Share on other sites More sharing options...
oni-kun Posted January 26, 2010 Share Posted January 26, 2010 I take the point about date() vs strftime() but I did do the suffix thing for strftime and this is what I used $day = strftime("%d",strtotime($orig_date)); if($day == 1 || $day == 21 || $day == 31) $dext = 'st'; else if($day == 2 || $day == 22) $dext = 'nd'; else if($day == 3 || $day == 23) $dext = 'rd'; else $dext = 'th'; $new_date = strftime("%B %e$dext %Y",strtotime($orig_date)); Yeah, the core does do pretty much the same thing anyway with date()'s parsing. Suppose ease over flexability. Quote Link to comment https://forums.phpfreaks.com/topic/189824-date-formating/#findComment-1001746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.