Tags
Asked 2 years ago
9 Jun 2021
Views 169
jqueryLearner

jqueryLearner posted

how to Convert this string to timestamp PHP ?

how to Convert this string to timestamp PHP ?
jaydeep

jaydeep
answered Apr 26 '23 00:00

To convert a string to a timestamp in PHP, you can use the strtotime () function. This function parses the date/time string and returns the corresponding Unix timestamp. Here is an example:



$date_string = '2022-05-01 10:30:00';
$timestamp = strtotime($date_string);
echo $timestamp;

In this example, the $ date_string variable contains the date/time string '2022-05-01 10:30:00'. The strtotime () function is then used to parse this string and convert it to a Unix timestamp, which is stored in the $ timestamp variable. Finally, the timestamp is echoed to the screen.

You can also use the DateTime class to convert a string to a timestamp in PHP. Here is an example:



$date_string = '2022-05-01 10:30:00';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $date_string);
$timestamp = $date->getTimestamp();
echo $timestamp;

In this example, the $ date_string variable contains the date/time string '2022-05-01 10:30:00'. The DateTime:: createFromFormat () method is then used to create a DateTime object from the string, using the format string 'Y-m-d H:i:s'. The [b] getTimestamp () [/b] method is then called on the DateTime object to get the Unix timestamp, which is stored in the $timestamp variable. Finally, the timestamp is echoed to the screen.
Post Answer