Date & Time

  • Description : Learn how to use the date function and how it is useful.
  • Language : PHP
  • Date Added : 24th October, 2009
Bookmark and Share

Dates can be used for many things. They might be used on a forum to inform users when a topic was posted, or to inform a user when he/she registered an account. It can also be used to display a message depending on the date.

Below is the basic format of the date function:

Code :
<?php $date = date("Y/m/d"); echo $date; ?>

So if the date today was the 24th October, 2009 the output would be:

Output:
2009/10/24

As you can see it displays the date. The letters used within the brackets define what format the date will appear in, the forward slashes merely separate the values to display the date more clearly. There are many available formats for the date that are extremely flexible, for a full list of possible formats click here and scroll down to the table.

I'll now use the date format combined with a switch statement to show an example of how the date function can be used:

Code :
<?php $date = date("l"); switch($date) { case "Monday","Tuesday","Wednesday","Thursday": echo "Hope you're having a good week!"; break; case "Friday": echo "Hope you have a good weekend!"; break; case "Saturday": "Hope you're having a good weekend!"; break; case "Sunday": echo "Did you enjoy your weekend?"; } ?>

So if today was a saturday the output would be:

Output:
Hope you're having a good weekend!

Although the code is fairly useless in terms of using it on a site, it does show you how the date function can be used. That concludes this tutorial, but in the next tutorial we will learn how to include other pages into one webpage!



Login Required

To post comments you must sign in to your account below!

User :
Pass :

If you do not currently have an account click here to create one now!