Join date: Sep 14, 2022


How to Convert String to Date Type in PHP?

Another important area of PHP programming is the date and time functions. In this post, we will let you know how to convert string to date type using PHP. Let’s see an example for each of them:

php convert string to date

$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time);

echo $newformat;

// 2003-10-16

It’s easy to convert string to date type in PHP. All you need is a function called 'date_default_timezone_get' which converts the string into compatible format of date that can be used further for displaying date to user.

php string to date

$s = '06/10/2011 19:00:02';

$date = strtotime($s);

echo date('d/M/Y H:i:s', $date);

The above one is the one of the example of converting a string to date.

echo $s ->format('Y-m-d');

The above one is another method

It is often required in PHP code to convert string to date type, like $date=strtotime("May 22, 2020");

convert string to date php

$s = '08/11/2010 19:37:02';

$date = strtotime($s);

echo date('Y-m-d H:i:s', $date);

If you want to convert String to Date in PHP, then a variety of methods might be helpful. One is to use the date_time() function. Another is to use strtotime(), and another way is by calling parse_dates().



More actions