All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Convert Timestamp To Date

Last Updated : Mar 11, 2024

PHP Convert Timestamp To Date

In this article we will show you the solution of PHP convert timestamp to date, in PHP, there are various functions used to present date into many different formats. Now as the title say, we can convert the UNIX timestamp into a date and also do vice versa.

We are going to use two functions here to convert timestamp to date and also covert to time. At first, we need the php date object.

  • date('d/m/y', timestamp) : we can convert the timestamp to date in d/m/y format. Here d refer to the day, m to month, and y to year.
  • echo date('H:i:s:a', time()) : we can convert the timestamp to time by H:i:s:a. Here H refers to the hour, I to minutes, s refers to the second, and a to am or pm.

We can also covert the date back to timestamp. To do this we can use strtotime() function.

Step By Step Guide On PHP Convert Timestamp To Date :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> php convert timestamp to date </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> Welcome to TalkersCode </h1>
    <h3> php convert timestamp to date </h3>
<?php
//convert timestamp to date
echo "<pre>" ;
echo date('d/m/y', 1681285941) ;
//convert timestamp to time
echo date('H:i:s:a', time());
//convert date to timestamp
$timestamp = strtotime('12-04-2023') ;
echo "<pre>" ;
echo $timestamp ;
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Attach an external CSS file using <link> tag
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here and also adding the inline CSS here.
  8. <?php to write php and the closing tag of ?>
  9. Date() object with the d/m/y format with the timestamp. Then using echo to display the date obtained by the timestamp
  10. Also using date() object with h:i:s:a to write the time with hour:minute:second:(am/pm) format. Then using echo to display it
  11. We can also covert date back to timestamp by using strtotime() function.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to Convert Timestamps to Date using PHP.

I hope this article on PHP convert timestamp to date helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪