logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ php

Php

by Admin _ May 28, 2022

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 & time

One 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
dDay of the month.twodigits with leading zeros01–31
jDay of the month without leading zeros1–31
SIncludes 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
DAbbreviatedtextual representation of a day,inthree lettersSun–Sat
lA full textual representation of a weekday.Sunday–Saturday
Month
FA full textual representation of a month, such as January or MarchJanuary–December
MAbbreviatedtextual representation of a month,inthree lettersJan–Dec
mNumeric representation of a month, with leading zeros01–12
nNumeric representation of a month, without leading zeros1–12
Year
YA full numeric representation of a year, 4 digitsE.g.1999or2003
yA two digit representation of a yearE.g.99or03
Time
AUppercase AnteMeridiem và PostMeridiemAMorPM
g12-hour format of an hour without leading zeros1–12
h12-hour format of an hour with leading zeros01–12
iMinutes with leading zeros00–59
sSeconds with leading zeros00–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?

Localization

This 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 times

It’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.

Share Tweet Linkedin Pinterest
Previous Post

Datetime::diff

Next Post

Time in php w3schools

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
bản khai đăng ký tên miền

Bản khai đăng ký tên miền

02/07/2022
phần mềm lập trình php tốt nhất

Phần mềm lập trình php tốt nhất

01/07/2022
ghi file trong php

Ghi file trong php

30/06/2022
thực tập part time php hà nội

Thực tập part time php hà nội

30/06/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Wth là gì

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.