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>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Now we close the HTML file with </html> tag
- we write <?php tag to write PHP within it.
- Creating a new DateTime() object with a timestamp .
- Using format() to get the date in Year-Month-Day format.
- Using echo to display the date.
- 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>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Now we close the HTML file with </html> tag
- we write <?php tag to write PHP within it.
- First create a variable with a timestamp.
- Use the date() function to get the date in the date-month-year format with strtotime() function.
- Using echo to display the date.
- 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.