PHP Date() function

巴扎黑
Release: 2023-03-01 09:48:01
Original
2752 people have browsed it

PHP’s date() function is used to format time or date.

PHP Date() Function

PHP Date() function formats timestamps into more readable dates and times.

Syntax

date(format,timestamp)

Parameter Description

format Required. Specifies the format of the timestamp.

timestamp Optional. Specify timestamp. The default is the current date and time.

PHP Date - What is Timestamp?

The timestamp is the number of seconds since January 1, 1970 (00:00:00 GMT). It is also called Unix Timestamp.

PHP Date - Format Date

The first parameter of the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here is a list of some available letters:

d - day of month (01-31)

m - current month as a number (01-12)

Y - current year (four digits)

You can find all the letters that can be used in the format parameter in our PHP Date reference manual.

You can insert other characters between letters, such as "/", "." or "-", so that you can add additional formats:

echo date("Y/m/d") ;
echo "
";
echo date("Y.m.d");
echo "
";
echo date("Y-m-d");
?>

The above code The output is similar to this:

2006/07/11
2006.07.11
2006-07-11

PHP Date - Add Timestamp The second parameter of the date() function specifies a timestamp. This parameter is optional. If you do not provide a timestamp, the current time will be used.

In our example, we will use the mktime() function to create a timestamp for tomorrow.

mktime() function returns a Unix timestamp for a specified date.

Syntax

mktime(hour, minute, second, month, day, year, is_dst)


To get the timestamp of a certain day, we only need to set the day parameter of the mktime() function:

< ;?php

$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));

echo "Tomorrow is ".date("Y /m/d", $tomorrow);
?>


The output of the above code is similar to this:

Tomorrow is 2006/07/12


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template