- Convert datetime to date php
- Converting a PHP timestamp to a date(time)
- In this article
- Getting a Timestamp
- Timestamp To Date Time Object
- Formatting a Date
- Formatting a Time
- Date with TimeZone
- Converting a Timestamp to Date Time
- Key Takeaways
- Convert One Date Format to Another in PHP
- Use date() and strtotime() Functions to Convert One Date Format to Another in PHP
- Use createFromFormat() and format() Functions to Convert One Date Format to Another in PHP
- Related Article — PHP Date
- Convert a Timestamp to a Readable Date or Time in PHP
- Use date() Function to Convert a Timestamp to a Date/Time in PHP
- Use setTimestamp() Function to Convert a Timestamp to a Date in PHP
- Use createFromFormat() Function to Convert a Timestamp to a Date in PHP
- Related Article — PHP Timestamp
- Related Article — PHP DateTime
Convert datetime to date php
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
Converting a PHP timestamp to a date(time)
In this PHP tutorial, we’re going to convert a timestamp in PHP to a date (optionally with time) using custom formatting. We’ll start by getting the current time stamp and display it using the simple date function. Next, we’ll use the current timestamp to create a Date Time object, format the date and display it. We’ll then view some of the date and time formatting options, followed by the Date Time Zone class. And finally, we’ll create a function to convert a timestamp to DateTime with time zone and date format parameters.
In this article
Getting a Timestamp
Let’s start with a basic PHP function to get the timestamp, we’ll use time() to get the current timestamp and date to format the output.
The time stamp saved in $now is similar to 1610246191, so we format it using date(‘Y-m-d’, $now) for a user friendly output. The timestamp returned is in seconds, see the microtime function to get a timestamp including micro seconds.
Timestamp To Date Time Object
While we can use the date function to format our date or timestamp, it isn’t the object oriented way of doing it. Let’s create a DateTime object and set the timestamp.
We can then output the date and time using the format method with the ‘c’ datetime parameter.
Formatting a Date
The format method can format a date in many different ways, common formats are: Y-m-d and d/m/Y .
The PHP manual has many more date formats available.
Formatting a Time
The DateTime class can also format the same timestamp in time only format.
We use the H:i:s(Hour, Minute, Second) format in the code above.
Date with TimeZone
The DateTime object can be set to use a specific time zone. We create a new DateTimeZone with a particular time zone and call the setTimeZone method on our DateTime object.
The DateTimeZone class has many timezone options. The PHP code example above displays the same timestamp using different time zones.
Converting a Timestamp to Date Time
Our final function is a combination of DateTime and DateTimeZone with 3 parameters: a timestamp, time zone and date/time format with default formatting.
Our custom PHP function timeStampToDateTime takes a timestamp as first parameter, which could be the current timestamp or any other time stamp (maybe a timestamp from a db). It uses the timezone and format to return a human readable date time string.
Key Takeaways
- We can use the time() function to get the current timestamp and the date function to format a timestamp.
- The DateTime class is the object oriented way of working with dates and times.
- The DateTimeZone class provides time zone functionality which can be used with the DateTime class to work with dates and times.
- The format , setTimeZone and setTimeStamp methods are the primary methods we can use to convert a timestamp to date/time.
- Related PHP functions: microtime
This is the footer. If you’re reading this, it means you’ve reached the bottom of the page.
It would also imply that you’re interested in PHP, in which case, you’re in the right place.
We’d love to know what you think, so please do check back in a few days and hopefully the feedback form will be ready.
Convert One Date Format to Another in PHP
- Use date() and strtotime() Functions to Convert One Date Format to Another in PHP
- Use createFromFormat() and format() Functions to Convert One Date Format to Another in PHP
In this article, we will introduce methods to convert one Date format to another in PHP.
Use date() and strtotime() Functions to Convert One Date Format to Another in PHP
The date() function converts a timestamp to a date . The correct syntax to use this function is as follows
$format is the specific format in which the date is converted.
$timestamp is an optional parameter. It gives the date according to the timestamp passed. If it omitted, then we will get the current date .
The function strtotime() is a built-in function in PHP. This function converts a date to the time. The correct syntax to use this function is as follows.
strtotime($dateString, $timeNow);
$dateString is a mandatory parameter, and it is the string representation of a date.
$timeNow is an optional parameter. It is the timestamp that is used for calculating relative dates.
php $originalDate = "2020-04-29"; //original date is in format YYYY-mm-dd $timestamp = strtotime($originalDate); $newDate = date("m-d-Y", $timestamp ); echo "The new date is $newDate."; ?>
We have used date() and strtotime() function to convert one date format to another. The function strtotime() has converted the original date to a timestamp. This timestamp is then converted to date of the required format using date() function.
Use createFromFormat() and format() Functions to Convert One Date Format to Another in PHP
The function createFromFormat() is a built-in function in PHP. This function converts a timestamp or date string to a DateTime object. The correct syntax to use this function is as follows.
DateTime::createFromFormat($format, $time, $timezone);
The variable $format is the format of the date, $time is the time or date given in string, and $timezone gives the time zone. The first two parameters are the mandatory parameters.
The format() function is used to format a date to the required format. The correct syntax to use this function is
$datetimeObject->format($formatString);
The parameter $formatString specifies the required format.
php $originalDate = "2020-04-29"; //original date is in format YYYY-mm-dd $DateTime = DateTime::createFromFormat('Y-m-d', $originalDate); $newDate = $DateTime->format('m-d-Y'); echo "The new date is $newDate."; ?>
Here, we have created a DateTime object using createFromFormat() function. The DateTime object then calls the format() function to convert one date format to another.
Related Article — PHP Date
Convert a Timestamp to a Readable Date or Time in PHP
- Use date() Function to Convert a Timestamp to a Date/Time in PHP
- Use setTimestamp() Function to Convert a Timestamp to a Date in PHP
- Use createFromFormat() Function to Convert a Timestamp to a Date in PHP
In this article, we will introduce methods to convert a timestamp to date in PHP.
- Using date() function
- Using setTimestamp() function
- Using createFromFormat() function
Use date() Function to Convert a Timestamp to a Date/Time in PHP
The date() function converts a timestamp to a human readable date or time . The correct syntax to use this function is as follows
It has two parameters. The parameter $format is the date-time format that the timestamp is converted to. The other parameter $timestamp is an optional parameter. It gives the date according to the timestamp passed. If it is omitted, it uses the current date by default.
php $date = date('d-m-Y H:i:s', 1565600000); echo "The date is $date."; ?>
The date format here is day-month-year , and the time format is hour:minute:second .
The date and time are 12-08-2019 08:53:20.
Use setTimestamp() Function to Convert a Timestamp to a Date in PHP
The built-in setTimestamp() converts the given timestamp to date or time . To set the format of the date we will use format() function.
$datetimeObject->setTimestamp($timestamp);
php $date = new DateTime(); $date->setTimestamp(1565600000); $variable = $date->format('U = d-m-Y H:i:s'); echo "The date and time is $variable."; ?>
The date and time are 1565600000 = 12-08-2019 08:53:20.
Use createFromFormat() Function to Convert a Timestamp to a Date in PHP
The built-in function createFromFormat() gets the date by passing the timestamp as a parameter to this function.
DateTime::createFromFormat($format, $time, $timezone);
The variable $format is the format of the date, $time is the time given in string and $timezone tells about the time zone. The first two parameters are the mandatory parameters.
php // Calling the createFromFormat() function $datetime = DateTime::createFromFormat('U', '1565600000'); // Getting the new formatted datetime $date= $datetime->format('d-m-Y H:i:s'); echo "The date and time is $date."; ?>
The format «d-m-Y H:i:s» displays both date and time .
The date and time are 12-08-2019 08:53:20.