The task is to convert DateTime to String using PHP. There are two ways to convert it.
- In the Format method, we can convert the DateTime object to a string.
- The DateTime to string program is programmed below:
Example:
PHP
<?php
$today = date("d/m/Y H:i:s");
if ($today) {
echo $today;
} else {
echo "can't display time";
}
?>
|
Output:
07/05/2021 17:57:38
2. By using list() method:
- The list() method is used to convert the DateTime object to string.
- The shorter way of list method is programmed below:
PHP
<?PHP
list($day, $month, $year, $hour, $min, $sec)
= explode("/", date('d/m/Y/h/i/s'));
echo $month . '/' . $day . '/' . $year . ' '
. $hour . ':' . $min . ':' . $sec;
?>
|
Output:
05/07/2021 05:58:48
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!