, 'opacity': false, 'speedIn': , 'speedOut': , 'changeSpeed': , 'overlayShow': false, 'overlayOpacity': "", 'overlayColor': "", 'titleShow': false, 'titlePosition': '', 'enableEscapeButton': false, 'showCloseButton': false, 'showNavArrows': false, 'hideOnOverlayClick': false, 'hideOnContentClick': false, 'width': , 'height': , 'transitionIn': "", 'transitionOut': "", 'centerOnScroll': false }); })
Home PHP PHP day count and date comparison
formats

PHP day count and date comparison

Published on May 13, 2008 by in PHP

Dealing with date based data can be a mind numbing experience. Calculation date differences, doing date comparisons can really stretch you to the limits at times. There is a variety of ways to deal with date based data but the first step usually is to convert it into a standard format that you can easily deal with. That format is the unix timestamp.

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.

Below is a code snippet for creating a timestamp from formatted date data. For this example the date_in variable contains a date in the format of YYYY-MM-DD and is passed from a previously executed form post.

  1. $datearray=explode("-",$_POST['date_in']);
  2. $year=$datearray[0];
  3. $month=$datearray[1];
  4. $day=$datearray[2];
  5. $mytimestamp=mktime(0,0,0,$month,$day$year);

Recently I discovered an easier way to get a date converted to a timestamp. This just goes to show you that a programmer is always learning. There will always be functions that are overlooked and underused.

strtotime() function this function will take an english based date format and convert it to timestamp. The code below illustrates how much easier this is.

  1. $mytimestamp=strtotime($_POST['date_in']);

One line of code! No exploding parts, just a quick and easy solution.

Calculate Number of days

Now that timestamps have been reviewed to show you some easy way to take two days and return a total number of days. The example below will take an arbitrary date of April 4, 2008 and May 12, 2008 (the current date I wrote the article)

  1. $time_A = strtotime("April 4, 2008");
  2. $time_B=strtotime(now);
  3. $numdays=intval(($time_B-$time_A)/86400)+1;

The number of seconds in a day is 86400 and the calculations usually shorts you a day so thats why we add a 1. This is do the fact the now() returns the current time which is not a complete day.

Another example of using date comparisons is expiring accounts in a database. If you can let the database do the work for you, DO IT, as anytime you can save code calculations the easier it is on you. This code will take the current date and compare it to the user_expdate and delete those records that are less than or equal to 0.

  1. $current_date=date("Y-m-d G:i:s");
  2.  
  3. $query="delete from {$db_prefix}users where user_expdate!='0000-00-00
  4. 00:00:00' and datediff(user_expdate,'".$current_date."')<= 0 ";
  5. if (!$result=$db->execute($query)){
  6. $db->ErrorMsg();
  7. }

Date comparisons

Date comparisons are now simple a matter of comparing the timestamps. Given 2 timestamps you can compare them and know very simply which is greater. Note: in the example below we forced the today variable to midnight so only days before the two timestamps are selected.

  1. $today=strtotime(date("Y-m-d")." 00:00:00");
  2. $datecheck = strtotime($year."-".$month."-".$day);
  3. if ($datecheck < $today){
  4. //find all dates before today and block them out
  5. }

Date comparisons in mysql can be done similarly. However mysql give a variety of date tools to work with as you are calculating dates.

The following code snipplet shows a way to delete code based on a timestamp comparison. Note: The code uses ADODB Lite calls. The $db->qstr function add's the appropriate quote code.

  1. $today=strtotime(date("Y-m-d")." 00:00:00");
  2.  
  3. $query="delete from {$db_prefix}users where user_expdate!='0000-00-00
  4. 00:00:00' and timestamp(user_expdate) < ".$db->qstr($today) ";
  5. if (!$result=$db->execute($query)){
  6. $db->ErrorMsg();
  7. }
  8.  

While these are some basic ways to do this, there are many other ways. I always welcome comments, feel free to add your own two cents.

Just make sure you register as No Spammers are allowed.

 
Tags:
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Brainstorms of a Webdev
credit