Date & Time in PHP

Date is a part of everyday life and is easy to work with without thinking. PHP also provides powerful tools for easier date manipulation.

Get the Time Stamp with the time () function in PHP

The time () function in PHP gives you all the information you need about the current date and time. It does not require parameters but returns an integer.

The integer number returned by the time () function represents the number of seconds that have elapsed since midnight January 1, 1970. Here is an example to illustrate:

  php print time (); ?> 

Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Picture 1 of Date & Time in PHP

If you look at the results, you may find it confusing. But PHP provides great tools to convert a timestamp into a form that people find convenient.

Convert Time Stamp with getdate () function in PHP

The getdate () function accepts a time stamp and returns an associated array containing date information. If you miss this time stamp, it works with the current time stamp when returned by time ().

The table below lists the elements contained in the conjugate array returned by getdate () in PHP:

Key Description Exampleseconds Seconds of minutes (0-59) 20 minutes Hours of hours (0 - 59) 29 hours Day time (0 - 23) 22 mday Months of the month (1 - 31) 11 wday Weekdays (0 - 6) ) 4 mon Year of the year (1 - 12) 7 year Year (4 digits) 1997 yday Year of the year (0 - 365) 19 weekday Day of the week Thursday month Month of the year January 0 Timestamp 948370048

By now, you can format this date and time in any format you want.

For example

Try the following example:

  php $date_array = getdate (); foreach ( $date_array as $key => $val ) { print "$key = $val
" ; } $mydate = getdate ( date ( "U" )); //bạn tham khảo hàm date() ở bên dưới print "Today: " ; echo "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]" ; ?>

Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Picture 2 of Date & Time in PHP

Convert Time Stamp with date () in PHP

The date () function in PHP returns a formatted string representing a date. You can practice various controls through the format that the date () function returns with a string parameter that you want to pass to it.

 date (format, timestamp) 

If you do not use (optionally) a Time stamp in the date () function, the current date and time will be used. Any other data you include in the format string passed to date () will be included in the return value.

The table below lists some code that a format string can contain:

Format Description Example 'am' or 'pm' in lowercase pm A 'AM' or 'PM' in upper case PM d Date of the month, number starting from 01 20 D Weekdays (3 letters) Thu F January h Hour (12h format, from 01) 12 H Hour (24h format, from 01) 22 g Hours (12h format, from 1) 12 G Hours (24h format, format 1) 22 i Minutes (0 - 59) 23 j Day of the month (form 1) 20 l (Lower 'L') Day of the week Thursday L Leap year ('1' for yes, '0' for full) 1 m Month of the year (number, form 01) 1 M Month of the year (3 letters) Jan r Date in RFC format 2822 Thu, 21 Dec 2000 16:01:07 +0200 n Month of the year (number, form 1) 2 s Seconds in hour 20 U Time stamp 948372444 y Year (2 digits) 06 Y Year (4 digits) 2006 z Date of the year (0 - 365) 206 Z Offset equals the seconds value from GMT +5

For example

You try the following example of the date () function in PHP:

  php // in ngày trong tuần echo date ( "l" ) . "
" ; // in ngày trong tuần, ngày trong tháng, tháng, năm, thời gian, AM hoặc PM echo date ( "l jS of F Y h:i:s A" ) . "
" ; // hiển thị October 3, 1975 là vào Friday echo "Oct 3,1975 was on a " . date ( "l" , mktime ( 0 , 0 , 0 , 10 , 3 , 1975 )) . "
" ; // sử dụng một hằng số trong tham số format echo date ( DATE_RFC822 ) . "
" ; // hiển thị date time dưới dạng giống như: 1975-10-03T00:00:00+00:00 echo date ( DATE_ATOM , mktime ( 0 , 0 , 0 , 10 , 3 , 1975 )); ?>

Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Picture 3 of Date & Time in PHP

Hope you understand how the date format and time are according to your requirements. A complete list of all date and time operation functions is provided in the chapter Function handling Date & Time in PHP

Follow tutorialspoint

Previous article: PHP for PERL Programmers

Next lesson: What is JavaScript

ncG1vNJzZmismaXArq3KnmWcp51ksaLAxGZkmqaUYnq1tcyeZKKmXaW1sQ%3D%3D