Forum Moderators: coopster

Message Too Old, No Replies

Finding months difference between two dates

Finding months difference between two dates

         

phparion

8:37 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi

I have written a PHP API that works with Amazon S3 simple storage API to automatically backup my two servers all sites and their databases and create a .tar.gz file for each server and shift it to Amazon Server.

The script creates a BUCKET (used for new folder / directory on amazon server) for each month as

monthName-YYYY

e.g

Sep-2007 for current month and will create Oct-2007
,Nov-2007 for upcoming two months respectively.

Each month folder has .tar.gz files for each server and for each day as backup is taken daily.

Now the problem with which I am stuck is that I want to keep LAST SIX MONTH backup on Amazon server and delete older backups.

for this I have written a script which works under cronjob every six month and will delete all folders / directory / bucket that is more than six months older.

I need help about counting the months difference between current month (in which the cron job runs) and the month which is used in folder / directory / bucket name as explained above.

e.g

if i have a backup of

Jan-2007
Feb-2007

and my cronjob runs today i.e in Sep, then I would want to calculate that above two backups are more than six months older and delete them.

I just need help about counting this NUMBER OF MONTHS because the year can be different too as in future the folder names will be with Jan-2008 etc

thank you very much.

Habtom

8:55 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi phparion.

The best way to handle this is to convert your dates to timestamps.

I have just responded to one similar thread of finding difference in age (years) [webmasterworld.com].

You can do the same thing for months. There seems nothing to be either than handling dates with timestamps.

Habtom

cameraman

8:57 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use strtotime [us2.php.net] to convert the Jan 2007 date, use again to convert '-6 months' and compare the two time stamps. If the folder time stamp is less than the -6 months time stamp, it's ready for deletion.

Romeo

2:25 pm on Sep 24, 2007 (gmt 0)

10+ Year Member



in Unix, if you won't like to deal with time stamps, to delete files and folders older than 190 days (roughly 6 months plus a few days), a basic shell command like

$ find /path/to/dir -mtime +190 -exec rm -r '{}'\;

could be used to recursively delete stuff.
Put it into a cron job and let it run once every month.

However, you should carefully test this stuff with an ... -exec ls -lr '{}'\; or ... -print first, so that the recursive `rm -r` will not blow away more than you want.

You may search for find+mtime+exec with Yahoo or Google for more.

Disclaimer: exercise caution, do extensive tests in a non-productive environment with no real data, and of course you are using this on your own risk.

Kind regards,
R.

phparion

5:33 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@Romeo: my backups are not on my server but on Amazon Server. My company uses Amazon Simple Storage service to keep backups in a safe place. And that works with an API and shell commands don't work. You have to do everything with XML requests.

I found a work around by using mktime() and a switch statement.

function IntMonth($m) {
$month = '';

switch($m) {
case "Jan":
$month = 1;
break;
case "Feb":
$month = 2;
break;
case "Mar":
$month = 3;
break;
case "Apr":
$month = 4;
break;
case "May":
$month = 5;
break;
case "Jun":
$month = 6;
break;
case "Jul":
$month = 7;
break;
case "Aug":
$month = 8;
break;
case "Sep":
$month = 9;
break;
case "Oct":
$month = 10;
break;
case "Nov":
$month = 11;
break;
case "Dec":
$month = 12;
break;

} //end switch

return $month;
} //end function

and

$d1=mktime(0,0,0,IntMonth(trim($dbox[0])),0,$dbox[1]);
$d2=mktime(0,0,0,date('m'),date('d'),date('Y'));
$months = floor(($d2-$d1)/2628000);

where dbox[1] had year value in it.

I tested it and it worked great.

thank you very much for your replies.

Habtom

5:36 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$d2 = mktime(0,0,0,date('m'),date('d'),date('Y')); was to put it in a similar format to $d1

It can just be written as follows:

$d2=time();

Habtom