Change Date Format in PHP



To change date format in PHP, the code is as follows −

Example

 Live Demo

<?php
   function string_convert ($my_date){
      $sec = strtotime($my_date);
      $my_date = date("Y-m-d H:i", $sec);
      $my_date = $my_date . ":00";
      echo $my_date;
   }
   $my_date = "23/11/2020 11:59 PM";
   string_convert($my_date);
?>

Output

1970-01-01 00:00:00

A function named ‘string_convert’ is used in PHP that takes a date as a parameter. The ‘strtotime’ function is used to convert English text timing into a UNIX timestamp −

$sec = strtotime($my_date);
$my_date = date("Y-m-d H:i", $sec);
$my_date = $my_date . ":00";

This date is printed on the screen. Outside the function, the date time is defined, and the function is called on this parameter and the output is displayed on the screen −

echo $my_date;
Updated on: 2020-08-17T12:10:00+05:30

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements