How to Get Only Date from Timestamp in PHP


How to Get Only Date from Timestamp in PHP

In this tutorial, we will learn how to get only date from timestamp in PHP. Timestamp or date-time object is very essential in creating dynamic web applications.

It helps us to know the exact time and store the time in our MySQL database while inserting the data in the database or updating the data in the database.

I recommend you all insert the timestamp in every table in your database while creating the applications.

It is often seen that we don’t have any particular date entry in our database but we need the exact date of the information we retrieve from the database.

As such, we can extract any date from the DateTime object from our database.

Also read, PHP difference between two dates in years, months and days

In order to extract or get only the date from the timestamp, we have to follow the below methods

  • strtotime()
  • date()

strtotime():- This function converts a textual date or human-readable date into UNIX Timestamp.

date():- This function converts a timestamp into a human-readable date with year and month and date.

The parameters of the date() function are described below

  • Y- The four-digit representation of the year.
  • m- The number representation of all the months from January to December.
  • d- The numeric representation of the date from Sunday to Saturday.

Illustration of the example of English Textual Date :

<?php

$date1 = '1st January 1988';

echo '<b>The timstamp format is:</b>    ' .strtotime($date1).'<br>';

echo '<b>The human readable date is:</b>    ' .date('Y-m-d',strtotime($date1));
?>

Output:-

The timstamp format is: 567993600
The human readable date is: 1988-01-01

Illustration of the example of the database timestamp :

<?php
$date2 = '2021-06-08 13:18:43';

echo '<b>The timstamp format is:</b>    ' .strtotime($date2).'<br>';

echo '<b>The extracted date from the above timestamp is:</b>   '.date('Y-m-d',strtotime($date2));
?>

Output:-

The timstamp format is: 1623158323
The extracted date from the above timestamp is: 2021-06-08

Conclusion:- I hope this tutorial will help you to understand the overview of date time objects and methods. If there is any doubt then please leave a comment below.


2 thoughts on “How to Get Only Date from Timestamp in PHP”

  1. I don’t know if it’s just me or if everybody else encountering
    problems with your website. It seems like some of the text in your posts are
    running off the screen. Can someone else please comment and let me know if
    this is happening to them as well? This may be a problem with my web browser because I’ve had this happen previously.
    Appreciate it

    Reply
  2. I just could not go away your web site before
    suggesting that I actually loved the standard
    info a person provide for your guests? Is gonna
    be back continuously to check up on new posts

    Reply

Leave a Comment