Time and date in PHP - ProgrammerTech


Time and date in PHP

  • Share this:
Time and date in PHP

Time and date in PHP

Time and date in php, today we will talk about date and time in PHP, when you build a specific site the refresh date and time should be updated every day and through php you can do that. php language provides a wide range of functions related to time and date, and the most important of these functions is "date".


date() function in php

The date() function converts the time into a more readable date and time. Your computer stores the date and time in a format called UNIX Timestamp that measures time in seconds since the beginning of the UNIX era. That is, midnight GMT January 1, 1970, which means January 1, 1970, and the time is 00:00:00 GMT. It is a series of numbers representing the number of seconds since then. Since this format is impractical for normal people (humans) to read, PHP converts this tense into a human-readable format and vice versa. Dates in human form to a timestamp that a computer can understand.

The general syntax of the date() function

date(format, timestamp)

The meaning of format in the date() function is the format of the specified date and time. timestamp is optional so the current date and time will be used.



Display today's date using php for this command.

$today = date ("d/m/Y");
echo $today;

When I run this code, it will give me the current date in which I write this lesson in the form.


php's date() function returns the current date and time according to the clock built into the web server on which the script was executed. Note that it will show you the date at the time you ran the code and not as it appeared to me in the previous result because it is the current date.


Functions that are used with date()

With so many different parameters, the date() function can cover the current date or time in different ways. Let's get acquainted with the most useful parameters:

  1. function date("y"): current year and today's date.
  2. function date("m"): the current month and today's date.
  3. Function date("F"): The name of the current month and today's date.
  4. function date("d"): the current day of the month and today's date.
  5. function date("l"): the name of the day and today's date.
  6. function date("w"): The number of the day of the week and the date of today.
  7. Function date("H"): the current hour in the current time.
  8. function date("i"): the current minute, timing the current.
  9. function date("s"): the current second in the current time.

An example that combines all of the above

    echo date("y") . '<br>';
    echo date("m") . '<br>';
    echo date("F") . '<br>';
    echo date("d") . '<br>';
    echo date("l") . '<br>';
    echo date("w") . '<br>';
    echo date("H") . '<br>';
    echo date("i") . '<br>';
    echo date("s") . '<br>';

When we run the above code on the server, we will get the result.


Parts of the date can be separated by inserting other characters such as (-), periods (.), slashes (/), or spaces, to add additional human readable formatting as in the following code.

echo date ("d/m/Y") . "<br>";
echo date ("d-m-Y") . "<br>";
echo date ("d.m.Y");

When running we will get the result


You can use date() in PHP to automatically update the duration of the copyright on your website, such that the copyright is held.


time() function

The time() function is used in order to get the current time as a Unix timestamp (which is the number of seconds since the beginning of the period 1/1/1970 00:00:00).

$kaissar = time();
echo ($kaissar);

When you run the code, it will give you a different numeric output depending on when you run the code.


We can convert this timestamp into a readable date, by passing it to the previous date() function.

    $kaissar = 1635846958;
    echo (date ("F d, Y h:i:s", $kaissar));

When we run this code, the result will be:

November 02, 2021 09:55:58


mktime() function

mktime() is used to generate the timestamp based on a specific date and time. If the date and time is not supplied, the timestamp for your current date and time will be returned.

The general form of the mktime() function

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

For example, the mktime() function can be used to find the name of the day of the week corresponding to a specific date. Just use the lowercase 'l' from 'L' with your timestamp, as shown in the following example.

The example displays the day starting on November 1, 2022.

echo date('l', mktime(0,0,0,11,2,2022));

In the previous code, it will give us the name of the day on 02/11/2022 and when the code is run, it will be in the form.


He reprinted to us that today on the given date is Wednesday. mktime() can be used to find out a specific date in the future after a specified period.

$futuer = mktime(0,0,0,date("m")+16,date("d"),date("Y"));
echo date('d/m/Y', $futuer);

The output will be with this code


This means that after 16 months from the moment I write the lesson, the date will be 02/03/2023.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com