i

PHP Tutorial

PHP Date and Time

Date and time functions are very important in any programming language as they allow us to keep track of the time whenever an action is performed. For example, when you create a PHP website where users login and log out. You need to have a proper track of their login date and time. PHP uses date() and time() functions to serve the purpose.

1 Time() and Getdate() function

Many times, you need the information about the current time and PHP time() function helps you to get all the information about current date with time(). Time function doesn’t need any argument and returns an integer when you use time() function. This integer is number of seconds since midnight GMT on 1st January 1970 and returned as time stamp.

For example:

echo time();

?>

It will give output like: 1575192825

It is very difficult to understand the timestamp, but PHP offers some tools to convert this timestamp into a readable form. We can convert the timestamp using getdate() and date() function. We will discuss getdate() in this section and date() function in the next section.

Getdate() accepts an optional timestamp and it returns an associative array that has the information about the date and time.

Some characters that are used for the format parameter of getdate() function are:

  1. seconds – It represents the second past minute from 0-59.

  2. minutes – It represents the minutes past the hour from 0-59.

  3. hours – It represents the hours of the day from 0-23

  4. mday – It represents the day of the month from 1-31.

  5. wday – It represents the day of the week from 0-6.

  6. year – It represents the year in four digits.

  7. yday – It represents the day of the year from 0-365.

  8. weekday – It represents the day of the week in string.

  9. month – It represents the month of the year in string.

  10. mon – It represents the month of the year from 1-12.

For example :

   $getdate_array = getdate();

    foreach ( $getdate_array as $key => $val )

{

    print "$key = $val
";

}

   $result_date  = "Today's date is: ";

   $result_date .= $getdate_array['mday'] . "/";

   $result_date .= $getdate_array['mon'] . "/";

   $result_date .= $getdate_array['year'];

   echo $result_date;

?>

Output of the above php script will be

seconds = 2
minutes = 45
hours = 9
mday = 1
wday = 0
mon = 12
year = 2019
yday = 334
weekday = Sunday
month = December
0 = 1575193502
Today's date is : 1/12/2019

2 Getting date using Date() Function

To format a date or time in PHP, date() function is used and it allows to format a timestamp into a user-friendly way to read date and time.

Syntax for date function is date(format, timestamp) where format specifies the format of the timestamp and timestamp has current date and time by default. Timestamp is an optional parameter which includes a sequence of characters with respect to date and time.

Some characters that are usually used for the format parameter of date function to get the date are:

  1. d – It represents the day of the month from 01 to 31.

  2. m – It represents the month from 01 to 12.

  3. Y – It represents the year in four digits.

  4. l – Lowercase letter ‘L’ represents the day of the week.

You can also insert “/”. “-“, “.” for using additional parameters or multiple parameters for the format.

For example:

echo “ Today’s date is “. date(“Y/m/d”). “
”;

echo “ Today’s date is “. date(“Y-m-d”). “
;

echo “ Today’s date is “. date(“Y.m.d”). “
”;

echo “ Day of the week is” .date(“l”);

?>

Output of the above php code will be:

Today’s date is 2019/12/01

Today’s date is 2019-12-01

Today’s date is 2019.12.01

Day of the week is Sunday

You can also copyright the year for your website using date() function. For example, your website is built in 2018 and you want to copyright it till date then you can write below code in php.

© 2018-

Output of the above php code will be : © 2018-2019

3 Getting Time using date() function

You can also get time using date() function and below are some characters used to get the time information using date() function.

  1. H – It represents the hour in 24-hour format from 00 to 23.

  2. h – It represents the hour in 12-hour format from 01 to 12.

  3. i – It represents the minutes past the hour from 00 to 59.

  4. s – It represents the seconds past the minutes from 00 to 59.

  5. a – It represents the time in am or pm i.e. Ante meridiem and Post meridiem.

For example:

echo “ The current time is “ . date(“h: i: s a”);

?>

Output of the above php script will be like :

The current time is 03:57:06 pm

You can also set the time zone if you need the time to be according to your location. For example, when you want to set the timezone to America or New York then you can write the below script.

date_default_timezone_set(“America/ New_York”);

echo “The time is” .date(“h:i:sa”);

?>

The output will be the current time according to the America and New York time zones.