Categories

Useful datetime functions with PHP and MySQL

by adesignguy Posted on 28/02/2016 23:55:05 | Category: Code

Here are a few useful date & time functions and especially useful when pulling a datetime field from a mySQL database using PHP.

<?php

//date and time
function datetime($input){
$input = date_create($input);
$input = date_format($input, 'd/m/Y H:i:s');
return $input;
}

// returns date and time in this format: 18/02/2016 18:54:34

echo datetime($row['date']);

?>

2: How to return a date in ISO8601 format. Useful for json+ld rich snippets.

<?php

$datetime = new DateTime($row['date']);

echo $datetime->format(DateTime::ISO8601);

//returns a date in ISO8601 format as so: 2016-02-18T18:54:34+0000

?>

3: Return the date with the day in long format.

<?php

//date time with day
function longdatetime($input){
$input = date_create($input);
$input = date_format($input, 'l d F Y H:i:s');
return $input;
}

//returns the date with day like so: Thursday 18 February 2016 18:54:34

echo longdatetime($row['date']);

?>