# Date and Time

• Print
This chapter is from the book

## Calculating the Difference Between Two Dates

``` \$century = mktime(12, 0, 0, 1, 1, 2001);
\$today = time();
\$difference = \$today - \$century;```

The epoche value that can be determined by time() and other PHP functions can be used to easily calculate the difference between two dates. The trick is to convert the dates into time stamps (if not already available in this format). Then the difference between these two time stamps is calculated. The result is the time difference in seconds. This value can then be used to find out how many minutes, hours, and days this corresponds to:

• Divide the result by 60 to get the number of minutes

• Divide the result by 60 * 60 = 3600 to get the number of hours

• Divide the result by 60 * 60 * 24 = 86400 to get the number of days

```<?php
\$century = mktime(12, 0, 0, 1, 1, 2001);
\$today = time();
\$difference = \$today - \$century;
echo ‘This century started ‘;
echo floor(\$difference / 84600);
\$difference -= 84600 * floor(\$difference / 84600);
echo ‘ days, ‘;
echo floor(\$difference / 3600);
\$difference -= 3600 * floor(\$difference / 3600);
echo ‘ hours, ‘;
echo floor(\$difference / 60);
\$difference -= 60 * floor(\$difference / 60);
echo " minutes, and \$difference seconds ago.";
?>```

The Difference Between Two Dates (timediff.php) Figure 3.5 The PHP code filled the month selection list with the appropriate number of days.

If you start with the number of days, round down each result and substract this from the result; you can also split up the difference into days, hours, and minutes.
{"ft_lang_label":"__label__en","ft_lang_prob":0.77290696,"math_prob":0.9878034,"size":2045,"snap":"2019-51-2020-05","text_gpt3_token_len":524,"char_repetition_ratio":0.15531603,"word_repetition_ratio":0.12643678,"special_character_ratio":0.2850856,"punctuation_ratio":0.13709678,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99370074,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-24T02:28:36Z\",\"WARC-Record-ID\":\"<urn:uuid:3c326ec4-e42e-4a54-8dfd-c3c550c1de21>\",\"Content-Length\":\"44636\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:196ed6d5-ea70-4d0a-936f-ecc005d54386>\",\"WARC-Concurrent-To\":\"<urn:uuid:39bd59c4-094e-4373-b579-5889e8a874a5>\",\"WARC-IP-Address\":\"159.182.165.92\",\"WARC-Target-URI\":\"http://www.informit.com/articles/article.aspx?p=417718&seqNum=14\",\"WARC-Payload-Digest\":\"sha1:FWUFY7W6XFP373X22GL3SKFCLUO7P3RA\",\"WARC-Block-Digest\":\"sha1:JJ4SVAITZWAKF73BMVCI4G3NDLIUBY64\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250614880.58_warc_CC-MAIN-20200124011048-20200124040048-00559.warc.gz\"}"}