php how to convert time to string code example

Example 1: Datetime to string php

<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>

Example 2: time to string in php

<?php
$date = '27/06/2020, 04:42:43 PM';
$date = str_replace('/', '-', $date);
echo date('F j, Y, g:i a',strtotime($date));

// output : June 27, 2020, 4:42 pm
?>

Example 3: Convert DateTime to String in PHP

phpCopy$dateFormat = new DateTime(); // this will return current date
echo $stringDate = $date->format(DATE_ATOM);

//output: 2020-03-08T12:54:56+01:00

Tags:

Php Example