PHP Date and Time

5 min read

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

PHP  date() функцийг огноо ба цагийг форматлахад ашигладаг.


The PHP Date() Function #

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

PHPdate()  функц нь цаг хугацааны тэмдэгийг илүү уншигдахуйц огноо, цаг болгон форматладаг.

Syntax- Синтакс #

date(format,timestamp)
Parameter Description
format – Формат Required. Specifies the format of the timestamp

Шаардлагатай. Цагийн тэмдэгийн форматыг тодорхойлно

timestamp -Цаг тэмдэг

Optional. Specifies a timestamp. Default is the current date and 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).

Date () функцын шаардлагатай формат параметр нь огноог (эсвэл цагийг) хэрхэн форматлахыг тодорхойлдог.

Here are some characters that are commonly used for dates:

Огноонд ихэвчлэн ашигладаг зарим тэмдэгтүүдийг энд оруулав.

  • d – Represents the day of the month (01 to 31) – Сарын өдрийг илэрхийлнэ (01-ээс 31) 
  • m – Represents a month (01 to 12) – Сарыг төлөөлдөг (01-ээс 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 add additional formatting. – “/”, “.”, Эсвэл “-” гэх мэт бусад тэмдэгтүүдийг тэмдэгтүүдийн хооронд нэмж нэмж форматлаж болно.

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

Доорх жишээнд өнөөдрийн огноог гурван өөр хэлбэрээр форматлав.

Example #

Жишээ #

<?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l"); ?>

PHP Tip – Automatic Copyright Year #

PHP зөвлөгөө – Автомат зохиогчийн жилийн эрх #

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

date() функцийг ашиглан вэбсайт дээрх зохиогчийн эрхийн жилийг автоматаар шинэчилнэ үү:

Example #

Жишээ #

&copy; 2010-<?php echo date("Y");?>

Get a Time #

Here are some characters that are commonly used for times:

Энд ихэвчлэн хэрэглэгддэг зарим тэмдэгтүүд байна:

  • H – 24-hour format of an hour (00 to 23) – Нэг цагаас 24 цагийн формат (00-оос 23 хүртэл)
  • h – 12-hour format of an hour with leading zeros (01 to 12) – Тэргүүлэх тэгүүдтэй нэг цагаас 12 цагийн формат (01-ээс 12 хүртэл)

  • i – Minutes with leading zeros (00 to 59) – Тэргүүлэх тэгүүдтэй минут (00-оос 59 хүртэл)
  • s – Seconds with leading zeros (00 to 59) – Тэргүүлэх тэгүүдтэй секунд (00-оос 59)

  • a – Lowercase Ante meridiem and Post meridiem (am or pm) – Жижиг үсэг Ante meridiem ба Post meridiem (am болон pm)

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

Доорх жишээнд тухайн цаг хугацааг тогтоосон форматаар гаргана:

Example
Жишээ
<?php echo "The time is " . date("h:i:sa"); ?>

Note that the PHP date() function will return the current date/time of the server!

PHP date () функц нь серверийн одоогийн огноо / цагийг буцааж өгөх болно гэдгийг анхаарна уу.


Get Your Time Zone #

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

Хэрэв та кодоос буцаж ирсэн хугацаа буруу байвал таны сервер өөр улсад байгаа эсвэл өөр цагийн бүсэд тохируулсан байх магадлалтай.

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

Тиймээс, хэрэв танд тодорхой байршлын дагуу зөв байх цаг хугацаа хэрэгтэй бол та ашиглахыг хүссэн цагийн бүсээ тохируулж болно.

The example below sets the timezone to “America/New_York”, then outputs the current time in the specified format:

Доорх жишээнд цагийн бүсийг “America / New_York” болгож, дараа нь тухайн цагийг заасан хэлбэрээр гаргана:

Example #

Жишээ #

<?php date_default_timezone_set("America/New_York"); echo "The time is " . date("h:i:sa"); ?>

Create a Date With mktime() #

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

Date () функцэд дурын цаг даралтын параметр нь цаг даралтыг зааж өгдөг. Орхигдсон тохиолдолд одоогийн огноо, цагийг ашиглана (дээрх жишээнүүдийн адил).

The PHP 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) and the time specified.

PHP mktime() функц нь огнооны Unix цагийн тэмдэгийг буцаана. Unix timestamp нь Unix Epoch (1970 оны 1-р сарын 1-ний 00:00:00 GMT цаг) ба заасан хугацааны хоорондох секундын тоог агуулдаг.

Syntax #

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

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

Доорх жишээ нь mktime()  функцын олон параметрүүдээс date()  функцтэй огноо, цагийг үүсгэдэг:

Example #

Жишээ #

<?php $d=mktime(11, 14, 54, 8, 12, 2014); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?>

Create a Date From a String With strtotime() #

Strtotime () ашиглан үгээс огноо үүсгэх #

The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

PHP strtotime () функц нь хүний ​​унших боломжтой огнооны үсгийг Unix timestamp (1970 оны 1-р сарын 1-ний 00:00:00 GMT-ээс хойшхи секундын тоо) болгон хөрвүүлэхэд хэрэглэгддэг.

Syntax #

strtotime(time, now)

The example below creates a date and time from the strtotime() function:

Доорх жишээ нь strtotime()  функцээс огноо, цагийг үүсгэдэг:

Example #

Жишээ #

<?php $d=strtotime("10:30pm April 15 2014"); echo "Created date is " . date("Y-m-d h:i:sa", $d); ?>

PHP is quite clever about converting a string to a date, so you can put in various values:

PHP нь үсэг үгийг огноо болгон хөрвүүлэхэд маш ухаалаг тул та янз бүрийн утга оруулж болно:

Example #

Жишээ #

<?php $d=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("next Saturday"); echo date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $d) . "<br>"; ?>

However, strtotime() is not perfect, so remember to check the strings you put in there.

Гэсэн хэдий ч strtotime() нь төгс биш тул та тэнд байрлуулсан мөрнүүдээ шалгахаа мартуузай.


More Date Examples #

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

Доорх жишээнд ирэх зургаан бямба гарагийн огноог оруулав.

Example #

Жишээ #

<?php $startdate = strtotime("Saturday"); $enddate = strtotime("+6 weeks", $startdate); while ($startdate < $enddate) { echo date("M d", $startdate) . "<br>"; $startdate = strtotime("+1 week", $startdate); } ?>

The example below outputs the number of days until 4th of July:

Доорх жишээнд 7-р сарын 4-нийг хүртэлх өдрийн тоог гаргана.

Example #

Жишээ #

<?php $d1=strtotime("July 04"); $d2=ceil(($d1-time())/60/60/24); echo "There are " . $d2 ." days until 4th of July."; ?>

Complete PHP Date Reference #

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

Бүх огнооны функцүүдийн талаар бүрэн лавлахыг хүсвэл манай PHP огнооны лавлагаа руу орно уу.

The reference contains a brief description, and examples of use, for each function!

Лавлагаа нь функц бүрийн товч тайлбар, ашиглалтын жишээг агуулсан болно!


Powered by BetterDocs

Leave a Reply