PhpMyanmar.com logo Php Tutorial
Home
Php Tutorial
 Echo
 If

File Upload
Show Myanmar Version
 show-myanmar-font
Printer Friendly Page

This lesson will teach you how to display the current time, formating PHP's timestamp, and show you all the various date arguments for reference purposes.

Date - The Timestamp

The date function always formats a timestamp, whether you supply one or not.

What's a timestamp?

  • Timestamp is the number of seconds from January 1, 1970 at 00:00. Otherwise known as the Unix Timestamp, this measurement is a widely used standard that PHP has chosen to utilize.
PHP Date - What Time Is It?

The date function uses letters of the alphabet to represent various parts of a typical date and time format. The letters we will be using in our first example are:

  • d: The day of the month. The type of output you can expect is 01 through 31.
  • m: The current month, as a number. You can expect 01 through 12.
  • y: The current year in two digits ##. You can expect 00 through 99

The rest of the options will be studied later.

For now let's use those above letters to format a simple date.

Other characters like a slash "/" can also be inserted between the letters to add additional formatting.

PHP Code:
<?php

echo date("d/m/y H:m:s");
echo "<hr>";

// 7 days; 24 hours; 60 mins; 60secs
$nextWeek = time() + (7 * 24 * 60 * 60);

echo 'Now: '. date('Y-m-d') ."<br>";
echo 'Next Week: '. date('Y-m-d', $nextWeek);

?>

You would see something like:

Display:
23/05/06 13:05:11
____________________
Now: 2006-05-23
Next Week: 2006-05-30

Supplying a Timestamp

The first argument of the date function tells PHP how you would like your date and time displayed.

The second argument allows for a timestamp and is optional.

This example below uses the mktime function to create a timestamp for tomorrow. To go one day in the future we simply add one to the day argument of mktime.

Note: These arguments are all optional. If you do not supply any arguments the current time will be used to create the timestamp.

  • mktime(hour, minute, second, month, day, year, daylight savings time)
PHP Code:
<?php
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo "Tomorrow is ".date("m/d/y", $tomorrow);
?>

Notice that one letter at a time is used with the function date to get the month, day and year.

For example the date("m") will return the month's number 01-12.

Display:
Tomorrow is 02/28/2010

Date - Reference

Now that you know the basics of using PHP's date function, you can easily plug in any of the following letters to format your timestamp to meet your needs.

Important Full Date and Time:
  • r: Displays the full date, time and timezone offset. It is equivalent to manually entering date("D, d M Y H:i:s O")
Time:
  • a: am or pm depending on the time
  • A: AM or PM depending on the time
  • g: Hour without leading zeroes. Values are 1 through 12.
  • G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
  • h: Hour with leading zeroes. Values 01 through 12.
  • H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
  • i: Minute with leading zeroes. Values 00 through 59.
  • s: Seconds with leading zeroes. Values 00 through 59.
Day:
  • d: Day of the month with leading zeroes. Values are 01 through 31.
  • j: Day of the month without leading zeroes. Values 1 through 31
  • D: Day of the week abbreviations. Sun through Sat
  • l: Day of the week. Values Sunday through Saturday
  • w: Day of the week without leading zeroes. Values 0 through 6.
  • z: Day of the year without leading zeroes. Values 0 through 365.
Month:
  • m: Month number with leading zeroes. Values 01 through 12
  • n: Month number without leading zeroes. Values 1 through 12
  • M: Abbreviation for the month. Values Jan through Dec
  • F: Normal month representation. Values January through December.
  • t: The number of days in the month. Values 28 through 31.
Year:
  • L: 1 if it's a leap year and 0 if it isn't.
  • Y: A four digit year format
  • y: A two digit year format. Values 00 through 99.
Other Formatting:
  • U: The number of seconds since the Unix Epoch (January 1, 1970)
  • O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours

Try to create several timestamps using PHP's mktime function and see what the outcome is.

Processing time -- 0.0144 seconds.

c33d4497204c2081fc9b77364a9b5d35