Dealing with dates & times is one of those things that can frustrate programmers a lot. At the same time, they are fundamental khổng lồ software development, used from everything from meta and how things are ordered lớn time-based triggers & lots in between.
Bạn đang xem: Php
Dates và times are prone lớn errors too. Handle them incorrectly, & they can confuse end-users và fellow programmers alike.
This is a quick guide to lớn dealing with dates và times specifically in the PHP programming language. It’s meant to be a reference lớn the most common needs you’ll have, like formatting & adjusting dates. It’s simple, but it’s likely going lớn cover 80% of your needs.
Table of Contents
This research is brought lớn you by tư vấn from Frontend Masters, darkedeneurope.com’ official learning partner.


Need front-end development training?
Frontend Mastersis the best place to get it. They have courses on all the most important front-end technologies. Interested in going full-stack? Here’s your best bet:
Take the Courses
Get the current date & timeOne thing lớn know is that the dates và times can be represented in three forms: a timestamp (i.e. Epoch time), a DateTime object, & a string.
First up, a recipe to lớn get the current date & time:
Day of the month | ||
d | Day of the month.twodigits with leading zeros | 01–31 |
j | Day of the month without leading zeros | 1–31 |
S | Includes the English suffix. Xem thêm: Phần Mềm Xem Camera Yyp2P - Hướng Dẫn Cài Đặt Phần Mềm Yyp2P | st,nd,rd,th(e.g.1st,2nd,3rd,4th) |
Weekday | ||
D | Abbreviatedtextual representation of a day,inthree letters | Sun–Sat |
l | A full textual representation of a weekday. | Sunday–Saturday |
Month | ||
F | A full textual representation of a month, such as January or March | January–December |
M | Abbreviatedtextual representation of a month,inthree letters | Jan–Dec |
m | Numeric representation of a month, with leading zeros | 01–12 |
n | Numeric representation of a month, without leading zeros | 1–12 |
Year | ||
Y | A full numeric representation of a year, 4 digits | E.g.1999or2003 |
y | A two digit representation of a year | E.g.99or03 |
Time | ||
A | Uppercase AnteMeridiem và PostMeridiem | AMorPM |
g | 12-hour format of an hour without leading zeros | 1–12 |
h | 12-hour format of an hour with leading zeros | 01–12 |
i | Minutes with leading zeros | 00–59 |
s | Seconds with leading zeros | 00–59 |
The DateTime object can be converted khổng lồ a timestamp:
getTimestamp(); // 1634139081But we can also get the current time in timestamp without constructing a DateTime object:
Construct a DateTime object of a specific time
What if we want khổng lồ construct a DateTime for a particular time, like July 14th, 2011? We can pass a formatted string date to lớn the constructor:Timezones
We can create a DateTime object that includes timezone information, like if we’re dealing with Pacific Standard Time, Eastern Daylight Time, etc.But say we want lớn convert a date and time that’s displayed in New York’s timezone lớn display Jakarta’s timezone instead?
LocalizationThis is a common way khổng lồ display date và time in the United States:
$ sudo apt-get install php-intlTo display a date và time in French, we can use IntlDateFormatter:
Besides IntlDateFormatter::FULL và IntlDateFormatter::SHORT, other popular formats are IntlDateFormatter::NONE, IntlDateFormatter::LONG,and IntlDateFormatter::MEDIUM.
If you use IntlDateFormatter::NONE for the time or in the third parameter, it means you don’t include the time in the format:
The p. And T are lớn separate period interval và time interval. Here’s how we can travel to the future:
Recurring dates and timesIt’s a common feature in calendar apps khổng lồ set a reminder that repeats every so often, like every two days or every week. We can use DatePeriod to represent a period of time:
diff($startDate); if ($dateInterval->invert==1) if ($dateInterval->y > 0) return $dateInterval->y . " years agon"; if ($dateInterval->m > 0) return $dateInterval->m . " months agon"; if ($dateInterval->d > 7) return (int)($dateInterval->d / 7) . " weeks agon"; if ($dateInterval->d > 0) return $dateInterval->d . " days agon"; }echo get_period_ago($date, $date2); // 5 days agoecho get_period_ago($date, $date3); // 2 weeks agoecho get_period_ago($date, $date4); // 7 months agoecho get_period_ago($date, $date5); // 2 years agoAfter getting the DateInterval object from the diff() method, make sure that the $startDate variable is in the past by checking the invert property. Then check the y, m, and d properties.
The full các mục of DateInterval object properties can be found here in the PHP docs.
Where vày you go from here?Now you have a little cheatsheet of common PHP recipes for when you find yourself working with dates và times. Need to lớn get the current date and time? Maybe you need to format a date a certain way, or include the local timezone, or compare dates. All of that is right here!
There are still more methods & functions about date và time that we haven’t discussed, of course — things lượt thích calendar-related functions and whatnot. Be sure to keep the PHP Manual’s Date & Time section close by for even more use cases and examples.