php date format code example

Example 1: php change date format

To convert the date-time format PHP provides strtotime() and date() function. We change the date format from one format to another.

Change YYYY-MM-DD to DD-MM-YYYY
<? php.
$currDate = "2020-04-18";
$changeDate = date("d-m-Y", strtotime($currDate));
echo "Changed date format is: ". $changeDate. " (MM-DD-YYYY)";
?>

Example 2: php get current year

$currentYear=date("Y");//2019

Example 3: php get current date and time

$today = date("F j, Y, g:i a");   // October 30, 2019, 10:42 pm
$today = date("D M j G:i:s T Y"); // Wed Oct 30 22:42:18 UTC 2019
$today = date("Y-m-d H:i:s");     // 2019-10-30 22:42:18(MySQL DATETIME format)

Example 4: date now php

date('Y-m-d H:i:s')

Example 5: php convert us date to european

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

Example 6: php date format

$originalDate = "2017-03-08";
$newDate = date("d/m/Y", strtotime($originalDate));

Tags:

Php Example