Brainstorms of a Webdev

Web development, PHP, Security, etc… etc…

  • Home
  • About

13

May

PHP day count and date comparison

Posted by tarnus  Published 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.

$datearray=explode("-",$_POST['date_in']);
$year=$datearray[0];
$month=$datearray[1];
$day=$datearray[2];
$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.

$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)

$time_A = strtotime("April 4, 2008");
$time_B=strtotime(now);
$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.

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

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.

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

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.

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

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.


Continue reading...

no comment

Search

Get Support

Categories

  • Domain Names (1)
  • Electronics (1)
  • Javascript (2)
  • mysql (1)
  • PHP (4)
  • Template Lite (2)
  • Uncategorized (1)

Archives

  • December 2008
  • May 2008
  • April 2008

Blogroll

  • Alien Assault Traders
  • Diana Botsford
  • Oznet
  • Sonnar Internet
  • Springfield Net

RSS Twitter: tarnus

  • Oh...waiting always sucks when your busy 09:05:17 AM May 23, 2008 from txt
  • Why do people refuse to listen 04:00:34 PM May 19, 2008 from im
  • Back to work... its a Monday 10:51:08 AM May 19, 2008 from im
  • Nothing like kids with nothing better to do than mess with my servers to ruin my sleep 12:52:10 AM May 18, 2008 from im
  • I got to get in better shape...I hate feelin winded 05:23:05 PM May 17, 2008 from txt

Meta

  • Register
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
May 2008
M T W T F S S
« Apr   Dec »
 1234
567891011
12131415161718
19202122232425
262728293031  

Recent Post

  • javascript change input box type to password
  • sql file splitter to the rescue
  • Palm Centro, my new phone
  • PHP day count and date comparison
  • Experiments in Domain Parking
  • Javascript Auto Focus Form Element
  • Variable Variables and Template Lite
  • Template Lite Modifiers

Recent Comments

  • Mikael Benjamin Ulstrup » Pro… in sql file splitter to the rescue
  • Online Reviews » Blog Archive… in Palm Centro, my new phone
© 2007 Brainstorms of a Webdev
Theme by Wired Studios, courtesy of Corvette Garage
Valid XHTML | Valid CSS 3.0
Powered by Wordpress