Last Updated:

PHP Date: features of work

Today in our article we will tell you about using PHP Date, figure out how to work with the format of the current date and time, talk about things that you may not have known. Happy reading!

PHP Date: features of work

Operation Description

Date is an operation that helps you format the system date or time (or rather its output). If you use format, the string will be returned following the pattern. In case you want to use and set a specific time, you need to use timestamp. If timestamp is not specified, the system time will be used.

We can conclude that timestamp is an optional thing, by default equal to the value of the system time.

In general, PHP contains a huge number of different functions. The most popular and common are:

  1. time ( ) - This function will return the current absolute time. Its value is equal to the number of seconds that have elapsed since -01 January 1970 (this date is traditionally considered to be the beginning of the UNIX era);
  2. getdate ( ) - This function displays all available date/time information. Displays an array that lists all the data at the selected time. An array may contain some of the following:
  • seconds — usage range 0-59;
  • minutes — usage range 0-59;
  • hours — usage range 0-23;
  • mday (selected day of the month) — usage range 0-30;
  • wday (selected day of the week) — usage range 0-6;
  • year;
  • month – simply enter the name of the month, for example March;
  • 0 is the absolute time.

There's also the date ( ) function, which, as we said above, works with time or date formatting. Below are some of its codes:

aDisplays "am" or "pm";
AOutputs "AM" or "PM";
dCurrent or selected day of the month (1-31);
DReduction of the day of the week (more precisely, its name);
FFull name of the selected or current month;
hClock (format 12);
HClock (format 24);
lFull name of the current or selected day of the week;
mMonth (from 1 to 12);
MThe name of the month (note that it is abbreviated);
sNumber of seconds (00-59);
tNumber of days in the selected or current month (28-31);
UAbsolute time;
wThe number of the day of the week (provided that 0 is Sunday and 6 is Saturday);
ySelected year (use of two digits);
YSelected year (use of four digits);
zDay of the year (0-365).
 

Another very important feature is called mktime ( ). It outputs an absolute, which will later be used with the already mentioned getdate or date functions. Can take a maximum of six arguments, with an integer value. Here's their order:

  1. First, hours are introduced, then minutes, then seconds.
  2. Then the month and its day are entered.
  3. The last entry point is the year.

Note that the use of mktime is not allowed until 1902.

There is also a very useful feature called checkdate ( ). It is responsible for verifying that the date you have chosen and entered is correct.

Her arguments are: day, month, year. The result is true if the date is correct, i.e. the following conditions must be met:

  • day The result is an integer that does not exceed the number of days in the current or selected month. Note that the leap years function handles correctly;
  • Month – The result is an integer between 1 and 12.
  • year is an integer (1-32767).

Return values

You can return an already formatted string with a date. Note that if you try to use a non-numeric value as a timestamp, you will simply receive an error (FALSE).

Examples

Example No.1 using the getdate function

<html>
<head>
<title>Using getdate()</title>
</head>
<body>
<?php
$d = getdate(); // current foreach time
used ( $d as $key => $val )
echo "$key = $val<br>";
echo "<hr>Ne now: $d[mday].$d[mon].$d[year]";
?>
</body>
</html>

Result of Example No.1

seconds = 34
minutes = 5
hours = 11
mday = 20
wday = 6
mon = 06
year = 2020
yday = 174
weekday = Saturday
month = June
Now: 6/20/2020

! Provided that today: 20.06.2020

Example No.2 using mktime

<html>
<head>
<title>Using mktime()</title>
</head>
<body>
<?php
echo date( "j of F Y, \a\\t g.i a, l", mktime( 14, 45, 6, 06, 20, 2020 ) );
?>
</body>
</html>

Result of Example No.2

20 of June 2020, at 2.45 pm, Saturday

Example No.3 using checkdate

if ( !checkdate( $month, 1, $year ) )
{
$d = getdate();
$month = $d[mon]
$year = $d[year];

}

We hope we were able to answer your questions on this topic. You can leave your feedback and suggestions in the comments.