Management
Get current date
See date
- In Iso 8601
$dateIso8601 = date('c');
- Epoch Timestamp in second precision with time
time()
Timestamp
Unix timestamp (in second) One hour ago
$time = time() - 3600;
Add
Add a day
$date = new DateTime();
$date->modify('+1 day');
Add seconds
$expirationTime = new DateTime();
$expirationTime->modify('+'.$cacheIntervalInSecond.' seconds')
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
- DateTime Diff with DateTime object
$diff = $sourceModifiedTime->diff($compiledModifiedTime);
$daysBetween = $diff->format('%a');
$secondsBetween = $diff->format('%s');
- Unix Timestamp methods (They are in seconds level)
round((time() - $timestampCreation)/60/60/24)
Greater than
For Datetime
$dateTime1 > $dateTime2