PHP Function Friday: date

I think the date function is extremely useful.  It gives your programs an awareness of WHEN they are.  You can do some pretty cool things like calculate this date from that date, make a calendar, or store information about when an action was taken.

Function and Syntax

string date(string $format  [, int $timestamp  ])

Manual Entry

http://php.net/manual/en/function.date.php

Notes and Use

There are two variables you can pass to the date function: the string format, and the timestamp.  What are those?  The string format uses various characters to define how the string the function returns will be formatted.  The table for which characters mean what is on the PHP Manual Page.  The timestamp is a Unix timestamp for a specific date/time.

Other Functions Referenced in Examples

Examples

Output Today’s Date and Time

This should just simply output today’s date.  I did a few formats based on January 1, 2009 at 4:52:39 PM.

echo date("Y-m-d H:i:s"); /* Should output '2009-01-01 16:52:39'.  Common programming format*/
echo date("F n, Y at g:m:s A") /* Should output 'January 1, 2009 at 4:52:39 PM'.*/

Put a Message on Your Website Every Monday

Do you hate Mondays?  Let everyone know.  This script checks to see if it’s Monday and then outputs a message if it is.  That ‘w’ in the date function is a numeric representation of the day of the week.  For instance: Wednesday is 3 and Sunday is 0.

if(date("w") == 1) {
   echo "Ugh.  It's Monday."
}

Examples Elsewhere on this Site:

Holiday Notices

Conclusion

Date functions are extremely useful when trying to figure out when you are (or better stated: when your program tries to figure out when it is).  Using date and time functions allow you to create a lot of automation in your scripts.  Have you got any useful implementations on this function?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • MySpace
  • Twitter
  • Sphinn
  • Reddit

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sponsor:

SponsoredTweets referral badge