Home arrow MySQL arrow Page 2 - Practical Date/Time examples with PHP and MySQL
MYSQL

Practical Date/Time examples with PHP and MySQL


In this article Mauricio shows us some examples of how to use the date/time features with MySQL and PHP including the UNIX timestamp and the PHP date_diff() function.

Author Info:
By: Mauricio Cuenca
Rating: 4 stars4 stars4 stars4 stars4 stars / 44
January 12, 2003
TABLE OF CONTENTS:
  1. · Practical Date/Time examples with PHP and MySQL
  2. · The UNIX timestamp
  3. · How old are you? (exactly)
  4. · Dates with MySQL
  5. · Date Addition and Subtraction
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Practical Date/Time examples with PHP and MySQL - The UNIX timestamp
(Page 2 of 6 )

The UNIX timestamp is the number of seconds elapsed since January 1, 1970 at 00:00:00. I use the timestamp mostly, it's better for doing arithmetical operations as it is just a number.

For me it’s easier to add or subtract thousands of seconds than doing the same with a date string. I’m going to use both timestamps and date strings although prefer the first.

Printing the current timestamp is as simple as <?php echo time(); ?>. This function alone doesn’t do much, the result is a number with more than nine figures, almost unreadable for the average user including me. I’m not going to enter into details about this topic as it has been covered in previous articles. Let’s go to the examples…

Calculating days, hours and seconds

PHP lacks date operation functions, but it provides the tools to do what we want to do. Say we want to know how many days, hours and seconds exist between two given dates, following is the function:

<?php

// The parameters of this function are the dates to be compared.
// The first should be prior to the second. The dates are in
// the form of: 1978-04-26 02:00:00.
// They also can come from a web form using the global $_POST['start']
// and $_POST['end'] variables.
function date_diff($str_start, $str_end)
{

$str_start = strtotime($str_start); // The start date becomes a timestamp
$str_end = strtotime($str_end); // The end date becomes a timestamp

$nseconds = $str_end - $str_start; // Number of seconds between the two dates
$ndays = round($nseconds / 86400); // One day has 86400 seconds
$nseconds = $nseconds % 86400; // The remainder from the operation
$nhours = round($nseconds / 3600); // One hour has 3600 seconds
$nseconds = $nseconds % 3600;
$nminutes = round($nseconds / 60); // One minute has 60 seconds, duh!
$nseconds = $nseconds % 60;

echo $ndays." days, ".$nhours." hours, ".$nminutes." minutes, ".$nseconds."
echo “seconds<br>\n";

}

// Test the function with several values
date_diff("1978-04-26", "2003-01-01");
date_diff("1984-10-24 15:32:25", "2003-01-01");
date_diff("2001-10-28 17:32:25", "2003-01-01 12:00:18");

?>


We could implement more options such as date validation etc., but I will add that in future articles.
This function does not need a lot of explanation, just multiplication and division. Now, let's seeanother example.
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 – More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials