Jump to content

Date formating


shahzad

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :o Just kidding, good to see use of the good old functions here or there.

Link to comment
Share on other sites

thanks both of you...

 

its working fine  :D

 

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!! :P

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.