Computer >> Computer tutorials >  >> Programming >> PHP

PHP program to change the date format


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

Example

<?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;