Php - Time (Date)

Management

Get current date

See date

$dateIso8601 = date('c');
time()

Timestamp

Unix timestamp (in second) One hour ago

$time = time() - 3600;

Add

$date = new DateTime();
$date->modify('+1 day');

String to Date

To DateTime

$dateInputFormat = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($dateInputFormat, "2017-09-18 12:20:22");

To Timestamp

$dateInputFormat = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($dateInputFormat, "2017-09-18 12:20:22");
$date->getTimestamp();

Date to String

From a timestamp

string date ( string $format [, int $timestamp = time() ] )

Example

date('l jS \of F Y h:i:s A',1391624456)
Wednesday 5th of February 2014 07:20:56 PM

From a datetime

$datetime->format('Y-m-d'); 

Diff

Get time difference in number of days

Unix Timestamp are in seconds

round((time() - $timestampCreation)/60/60/24)

Greater than

For Datetime

$dateTime1 > $dateTime2

Documentation / Reference


Powered by ComboStrap