home ~ previous

PHP

Dates: finding the difference between two dates

At some point, some herbert will ask you to come up with two dates and work out the difference between them. Seriously, they will, right when you least expect it. It's what they do!

This method is presented in the RFC822 format.

It is worth learning/hanging onto this code and experimenting with the different format characters ("D, d m Y G:i:s O"). The docs for this can be found here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
#date_default_timezone_set('Europe/London');
$BST_time = date_default_timezone_set('Europe/London');

######### This creates the requested format (RFC822, it would seem is the replacement for RFC2822???)
$datestamp_today = date(DATE_RFC822); 
$time = strtotime($datestamp_today);

$time1 = date("D, d m Y G:i:s O", strtotime('0 minutes', $time));
$time2 = date("D, d m Y G:i:s O", strtotime('+30 minutes', $time));

####### this isn't great - I am declaring the same thing again - but it was the easiest way of getting the variables to behave as I want them to. I will update this at somepoint.
$present_time = strtotime('0 minutes', $time);
$future_time = strtotime('+30 minutes', $time);
$difference_time_in_minutes = round(abs($present_time - $future_time) / 60,2). " minutes";

?>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"  />
	<title>Minutes between two times</title>
	<style>
		.wrapper {width:800px;font-family: Arial;font-size:24px;line-height: 36px;margin: 20px auto;background-color: #ebebeb;padding:20px 20px 20px 20px;}
		blockquote {padding:20px; background-color: #fff;}
	</style>
</head>
<body>
	<div class="wrapper">
		<p>Your current timezone is  <?php echo(date_default_timezone_get()); ?></p>
		<p>The time zone abbreviation is <?php echo(date('T')); ?></p>
		<blockquote><em>did you know ...</em><br />British Summer Time (BST) begins Sunday, 25 March 2012<br />and ends on Sunday, 28 October 2012</blockquote>
		<p>The current time is: <?php echo($time1); ?></p>
		<p>In half an hours time, it will be: <?php echo($time2);?></p>
		<p>The difference between these two times is: <?php echo($difference_time_in_minutes); ?></p>
	</div>
<body>
</html>

home ~ previous

Rohan Leach .... thoughtful