All TalkersCode Topics

Follow TalkersCode On Social Media

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

Convert Timestamp To Date PHP

Last Updated : Mar 11, 2024

Convert Timestamp To Date PHP

In this article we will show you the solution of convert timestamp to date PHP, we can convert timestamps using date using two different methods using php.

In this tutorial, we will use functions like format() and date().

  • Format(): using this function we can format the timestamp into the specified date format.
  • Date(): by using the php function convert the timestamp with a date format.

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

We will use two different methods to convert timestamps to date.

Method 1

In the example below, we convert the timestamp to date using the format() method.

<!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> convert timestamp to date php </title>
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> convert timestamp to date php </h2>
    <?php
$date = new DateTime("2023-02-18 08:40:38") ;
$formatDate = $date->format("y-m-d") ;
echo $formatDate ;
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an 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 above now the <head> tag is used to contain 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. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Now we close the HTML file with </html> tag
  8. we write <?php tag to write PHP within it.
  9. Creating a new DateTime() object with a timestamp .
  10. Using format() to get the date in Year-Month-Day format.
  11. Using echo to display the date.
  12. Close the php code with ?> tag.

Method 2

In the example below, we convert timestamps to date using the date() method.

<!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> convert timestamp to date php </title>
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> convert timestamp to date php </h2>
    <?php
$timeStamp = "2023-02-18 08:40:38" ;
$formatedView = date("M . d . y",strtotime($timeStamp)) ;
echo $formatedView ;
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an 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 above now the <head> tag is used to contain 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. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Now we close the HTML file with </html> tag
  8. we write <?php tag to write PHP within it.
  9. First create a variable with a timestamp.
  10. Use the date() function to get the date in the date-month-year format with strtotime() function.
  11. Using echo to display the date.
  12. Close the php code with ?> tag.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to convert timestamp to date using PHP.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪