#native_company# #native_desc#

Date Calculating

By Gregory
on November 4, 2001

Version: 2.02 & 3.23.32

Type: Sample Code (HOWTO)

Category: Calendars/Dates

License: GNU General Public License

Description: This is my first posting to PHP Builder. I was in need of a date calculation snippet that would just add a the number: 30,60,90, or 120 to a date. With help from Sams publishing: “PHP and MySQL” this is what I came up with.

Function: Return an expiration date based on length of service.

First we need our date from our database in timestamp form:
** Please note that your mysql recordset return object will be different **


"SELECT UNIX_TIMESTAMP(date_field) as ts, duration FROM memberships"

Now we convert this timestamp into a date 
(only necessary if we want to display the original date):

/* part one */

// get timestamp from recordset result
$date_time_array  = getdate($hashish['ts']);

// get parts
$month =  $date_time_array["mon"];
$day =  $date_time_array["mday"];
$year =  $date_time_array["year"];

// format
$formatted_date = $month."/".$day."/".$year;

// -----------------------------------------------------------

/* part two */

// create calculated timestamp using our duration value 
// returned from our recordset. 
$getts = mktime("","","",$month,$day + $hashish['duration'],$year);

// using the new timestamp get a date array
$add_dt_array = getdate($getts);

// get parts
$month =  $add_dt_array["mon"];
$day =  $add_dt_array["mday"];
$year =  $add_dt_array["year"];
// format
$end_date = $month."/".$day."/".$year;

// This will return: 1/2/2002 with the date 11/3/2001
// and adding a duration of 60 days.

// We can expand on this by using other time/date calculations,
// but if we want to just perform simple arithmatic on dates this will do.