Create Date Php

The PHP date() function is used to format a date and/or a time.

Bạn đang xem: Create date php

The PHPhường. Date() Function

The PHP.. date() function formats a timestamp to a more readable date & time.


Parameter Description format Required. Specifies the format of the timestamp timestamp Optional. Specifies a timestamp. Default is the current date & time

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Get a Date

The required format parameter of the date() function specifies how to format the date (or time).

Here are some characters that are commonly used for dates:

d - Represents the day of the month (01 lớn 31) m - Represents a month (01 to 12) Y - Represents a year (in four digits) l (lowercase "L") - Represents the day of the week

Other characters, like"/", ".", or "-" can also be inserted between the characters to lớn add additional formatting.

The example below formats today"s date in three different ways:

";emang đến "Today is " . date("Y.m.d") . "
";emang lại "Today is " . date("Y-m-d") . "
"; emang lại "Today is " . date("l");?>

PHPhường Tip - Automatic Copyright Year

Use the date() function to automatically update the copyright year on your website:

Get a Time

Here are some characters that are commonly used for times:

H - 24-hour format of an hour (00 lớn 23) h - 12-hour format of an hour with leading zeros (01 khổng lồ 12) i - Minutes with leading zeros (00 lớn 59) s - Seconds with leading zeros (00 lớn 59) a - Lowercase Ante meridiem and Post meridiem (am or pm)

The example below outputs the current time in the specified format:

Get Your Time Zone

If the time you got back from the code is not correct, it"s probably because your VPS is in another country or set up for a different timezone.

Xem thêm: Tải Game Afk 3Q Trung Quốc Trên Điện Thoại, 3Q Afk Apk 1

So, if you need the time to lớn be correct according to lớn a specific location, you can set the timezone you want lớn use.

The example below sets the timezone khổng lồ "America/New_York", then outputs the current time in the specified format:

Create a Date With mktime()

The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date và time will be used (as in the examples above).

The PHPhường mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) và the time specified.


The example below creates a date & time with the date() function from a number of parameters in the mktime() function:

Create a Date From a String With strtotime()

The PHP. strtotime() function is used lớn convert a human readable date string inkhổng lồ a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).


"; $d=strtotime("next Saturday");echo date("Y-m-d h:i:sa", $d) . "
"; $d=strtotime("+3 Months");emang lại date("Y-m-d h:i:sa", $d) . "

However, strtotime() is not perfect, so rethành viên lớn check the strings you put in there.

More Date Examples

The example below outputs the dates for the next six Saturdays:

Complete PHP Date Reference

For a complete reference of all date functions, go to our complete PHP Date Reference.

The reference contains a brief mô tả tìm kiếm, and examples of use, for each function!

PHPhường Exercises

Test Yourself With Exercises


Use the correct date function to lớn output the weekday name of today (monday, tuesday etc.).

Tabs Dropdowns Accordions Side Navigation Top Navigation Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Range Sliders Tooltips Slideshow Sort List

Report Error

If you want to report an error, or if you want lớn make a suggestion, vì not hesitate to skết thúc us an e-mail:


Top Tutorials HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Pythuôn Tutorial W3.CSS Tutorial Bootstrap Tutorial PHPhường Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Top References HTML Reference CSS Reference JavaScript Reference Squốc lộ Reference Pykhiêm tốn Reference W3.CSS Reference Bootstrap Reference PHPhường Reference HTML Colors Java Reference Angular Reference jQuery Reference
Top Examples HTML Examples CSS Examples JavaScript Examples How To Examples Squốc lộ Examples Python Examples W3.CSS Examples Bootstrap Examples PHPhường Examples Java Examples XML Examples jQuery Examples
Web Courses HTML Course CSS Course JavaScript Course Front End Course Squốc lộ Course Pyhạn hẹp Course PHPhường Course jQuery Course Java Course C++ Course C# Course XML Course Get Certified » is optimized for learning và training. Examples might be simplified lớn improve sầu reading & learning. Tutorials, references, and examples are constantly reviewed lớn avoid errors, but we cannot warrant full correctness of all nội dung. While using, you agree to have read and accepted our terms of use, cookie và privacy policy. Copyright 1999-2021 by Refsnes Data. All Rights Reserved. is Powered by W3.CSS.