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:
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 948370048By 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:
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 +5For 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:
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
You should read it
- Strftime () function in Python
- 4 ways to use the Clone Stamp tool in Photoshop
- NOW function - The function returns the current date and time in Excel
- Function handles DATE / TIME in SQL - Part 1
- Instructions on how to use Clone Stamp in Photoshop
- Date and Time in C ++
- How to stamp copyright PDF files
- Strptime () function in Python
May be interested
- Instructions to hide date and time from Windows 11 Taskbarwindows 11 version 24h2 allows you to hide the date and time from the taskbar if users want to tidy up the screen interface.
- Quickly insert current date and time into a cell in Excelin excel, you can quickly fill out current date, month, and time information as static or dynamic values. the static value is the date and time value at the time of filling without automatically updating on subsequent days when you open or perform calculations on a spreadsheet.
- The easiest way to adjust the date and time on Windows 11in most cases, windows will automatically synchronize the date and time, but for some reason this information is not displayed correctly on windows 11, you can change it manually, article below will share with you how to adjust the date and time on windows 11 with specific instructions.
- The date command in Windowsthe date command displays or sets the system date. if used without parameters, the date command displays the current system date setting and prompts you to enter a new date.
- Fix computer error that does not automatically update standard date and timethe computer displaying the wrong time or not automatically updating the standard date and time will lead to the system not working properly, not being able to access the internet or use some applications, etc. at this time, it may be possible. follow the steps to fix the error of your computer not automatically updating the standard date and time to ensure your device displays the correct time, avoiding unnecessary troubles.
- How to manage date and time in React using Moment.jsmoment.js is a great library for efficient date and time management in react apps. here are detailed instructions.
- How to fix the date #VALUE error in Excelwhen entering a date-related formula in excel, some people are reported with the #value error, affecting the results in excel files.
- Date time functions in Excelexcel supports you to process and calculate quickly with the functions that excel provides such as calculation functions, date functions ... one of them is the function of time to help you handle the prices. time value: hour, minute, second conveniently
- How to automatically update the Excel file editing timewhen inserting and editing time excel file, you will control the latest time to edit the file if shared with many people.
- How to determine the posting date of a website or an information on the Internetwhen you do research on a topic or simply need to look up and synthesize certain information, it's important to make sure your source is up to date.