Php Date and time function

In this tutorial you will learn how to extract or format the date and time in PHP. The Php Date and time are some of the most frequently used operations in PHP. PHP has builtin date and time function. So let get started with the php date and time function

Php Date() function

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

The computer stores dates and times in a format called UNIX Timestamp, which measures time as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT ).

Since this is an impractical format for humans to read, PHP converts a timestamp to a format that is readable to humans and dates from your notation into a timestamp the computer understands. The syntax of the PHP date() function can be given with.

Syntax

date(format, timestamp)

The format parameter in the date() function is required which specifies the format of returned date and time. However the timestamp is an optional parameter, if not included then current date and time will be used. The following statement displays today’s date:

Example

<?php
// echo today date
echo date("d/m/Y");
?>

Formatting the Dates and Times with PHP

The format parameter of the date() function is in fact a string that can contain multiple characters allowing you to generate a date string containing various components of the date and time, like day of the week, AM or PM, etc. Here are some the date-related formatting characters that are commonly used in format string:

  • d – Represent day of the month; two digits with leading zeros (01 or 31)
  • D – Represent day of the week in text as an abbreviation (Mon to Sun)
  • m – Represent month in numbers with leading zeros (01 or 12)
  • M – Represent month in text, abbreviated (Jan to Dec)
  • y – Represent year in two digits (08 or 14)
  • Y – Represent year in four digits (2008 or 2014)

You can find more formate here

The PHP time() Function

The time() function is used to get the current time as a Unix timestamp (the number of seconds since the beginning of the Unix epoch: January 1 1970 00:00:00 GMT).

<?php
// This will return the current timestamp
echo time();
?>

The above example produce the following output.

1566407823

We can convert this timestamp to a human readable date through passing it to the previously introduce date() function.

<?php
$timestamp = 1566407823;
echo date(“d-m-Y H:i:s”, $timestamp);
?>

The above example produce the following output.

21-08-2019 19:17:03

The PHP mktime() Function

The mktime() function is used to create the timestamp based on a specific date and time. If no date and time is provided, the timestamp for the current date and time is returned.

The syntax of the mktime() function can be given with:

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

The following example displays the timestamp corresponding to 10:30:55 pm on May 02, 2019:

<?php
// Create the timestamp for a particular date
echo mktime(10, 30, 55, 2, 05, 2019);
?>

The above example produce the following output.

1549359055

The mktime() function can be used to find the weekday name corresponding to a particular date. To do this, simply use the ‘l’ (lowercase ‘L’) character with your timestamp, as in the following example, which displays the day that falls on May 05, 2019:

<?php
// Get the weekday name of a particular date
echo date('l', mktime(10, 30, 55, 5, 05, 2019));
?>

The above example produce the following output.

Sunday

If you have found this article useful, share this article with your friends and leave comment if you have any question. Your responses are also highly appreciated.

7 Comments

  1. ARIPIPRAZOLE buy said:

    I am just writing to make you understand what a outstanding experience my wife’s girl obtained using your webblog. She noticed so many pieces, which included what it is like to possess an excellent helping spirit to let other people with no trouble learn specific specialized things. You truly surpassed readers’ expected results. Thank you for providing these priceless, trusted, informative as well as fun tips about this topic to Lizeth. https://abilifyaripiprazoles.com/

    January 8, 2021
    Reply
  2. finasteride cheap said:

    I am just writing to make you understand what a notable experience my wife’s girl had using your web site. She mastered so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn some problematic things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth. https://harrington-service.com/

    January 8, 2021
    Reply
  3. endep no rx said:

    I enjoy you because of all of the effort on this blog. Betty delights in carrying out internet research and it’s easy to see why. A lot of people hear all about the compelling form you convey both interesting and useful guidelines by means of the blog and attract contribution from other individuals about this area and my child has always been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a dazzling job. https://endepamitriptyline.com/

    January 8, 2021
    Reply
  4. nortriptyline no rx said:

    I’m commenting to let you be aware of of the beneficial discovery my child encountered browsing the blog. She came to understand a lot of details, including how it is like to have a great coaching character to get certain people completely comprehend a variety of complex issues. You actually did more than her desires. I appreciate you for coming up with such essential, dependable, edifying and cool guidance on that topic to Ethel. https://pamelornortriptyline.com/

    January 8, 2021
    Reply
  5. generic zyprexa said:

    Thanks for the tips on credit repair on all of this blog. What I would advice people is to give up the mentality that they can buy now and pay later. As a society we tend to do this for many things. This includes vacations, furniture, and items we want. However, you need to separate your wants from all the needs. While you are working to improve your credit score you have to make some sacrifices. For example you can shop online to save money or you can go to second hand stores instead of expensive department stores for clothing. https://zyprexaolanzapine.com/#

    January 9, 2021
    Reply
  6. venlor price said:

    A lot of thanks for all your valuable hard work on this web page. Kate loves getting into investigation and it’s really easy to understand why. I know all of the dynamic medium you make insightful solutions on this web blog and as well as foster participation from people on that idea so our girl is certainly discovering a lot of things. Have fun with the remaining portion of the new year. You are conducting a powerful job. https://venlorz.com/#

    January 10, 2021
    Reply
  7. riser do kryptowalut said:

    A lot of thanks for all your valuable hard work on this web page. Kim loves getting into investigation and it’s really easy to understand why. Many of us know all of the dynamic medium you make insightful steps on this web blog and as well as improve participation from people on that idea so our girl is certainly discovering a lot of things. Have fun with the remaining portion of the new year. You are conducting a powerful job. https://allegrolokalnie.pl/oferta/riser-pci-e-008c-5w-mniej-od-009-najstabilniejszy

    January 16, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *