Working with Dates & Timestamps in PHP

Working with Dates and Timestamps is always trying, but using UNIX timestamps can save you a lot of time and hassle!

Because they can be converted to simple numbers and then you can just worry about the number.

You add/subtract and get durations etc. Then convert the result to Date in whatever format.

For example, the code below finds out how much time in minutes has passed between a timestamp from a document, and the current time.

$date  = $item['pubdate'];
(etc ...)

$unix_now = time();
$result = strtotime($date, $unix_now);
$unix_diff_min = (($unix_now  - $result) / 60);
$min = round($unix_diff_min);

Get the date of Monday of the Current Week:

$mondayOfCurrentWeek = strtotime ('last monday', strtotime('next sunday'));
// Display the date - example, June 13, 2011, 12:00 am
echo date("F j, Y, g:i a", $mondayOfCurrentWeek );

One Reply to “Working with Dates & Timestamps in PHP”

Leave a Reply