converting date time to string php 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: date to string php

$date_string_prepared = date_create("2020-08-07");
$date_string = $date_string_prepared->format("d M Y");
// result 07 Jul 2020

Example 4: date to string in php

Date to string

$date = "2021/03/13";
$newdate= date('d M, Y', strtotime($date));
echo $newdate;

Tags:

Php Example