Working with Dates using PHP Tutorial

This is a copy of the tutorial that I wrote on SlashDot that gives a brief overview of how to manipulate dates using PHP. This example comes from a script that I wrote for a customer who wanted to expire downloads and clean up the directory tree based on the number of days that passed since the download was requested. This script is called by a cron job at midnight every day. To do this, I created a MySQL table in their database called “downloads” that is used to keep track of the dates and other information that was gathered at the time of the user’s request. But, since that is another topic for another day, I will refrain from discussing the sign-up form and database structure. At the top of the script, I put in the include for the file that contains my MySQL connection class. As you will see in later code, this class has a method (doSQL()) that returns an array that contains the number of records in the 1st (0th) element and the resource that holds the data requested in the 2nd (1st) element…

<?php require './MySQL.php'; ?>

… then I set the appropriate directory that contains the downloads and retrieve the content resource into a variable for parsing…

$dir = './downloads';
$dir_content = scandir($dir);

…then I set the timezone to my locale and create a variable ($myDate) that contains the current date in a format that can be used in MySQL…

$myTZ = 'America/Detroit';
$myNewTZ = new DateTimeZone($myTZ);
$Date = date_create(date("Y-m-d H:i:s"));
date_timezone_set ($Date, $myNewTZ);
$myDate = date_format($Date, "Y-m-d H:i:s");

Once this is accomplished, I convert the date into the julian date format by parsing out the date into an array that contain the Month, Day and Year and storing it in a variable ($julian_date)…

$Date_Array = date_parse($myDate);
$julian_date = gregoriantojd($Date_Array["month"], $Date_Array["day"], $Date_Array["year"]);

Then I loop through the entire directory structure looking for the julian date of the date of download. The directory tree is ./downloads/$julian_date/[user_email]/[].

$db = &new MySQL();
$email = '';
$failed_subject = 'Expired Downloads Procedure failed!';
$found_subject = 'Expired Downloads';
$message = '';
$filename = '/<>';
foreach($dir_content as $key => $content) {
if($julian_date - $content > 30 && $content !== '.' && $content !== '..') {
$SQL = "select `download_id`, `download_dir`, `user_name`, `email` from `downloads` where `download_dir` like '%" . $content . "%';";

$pArray = @$db->doSQL($SQL);
$res = $pArray{0};
$cnt = $pArray{1};
if($cnt > 0) {
while($row = mysql_fetch_array($res)) {
if(!removeResource($row['download_dir'] . $filename)) {
print("Delete unsuccessful. $message\n");
} else {
$message .= 'User: ' . $row['user_name'] . ' (' . $row['email'] . ') ' . $row['download_dir'] . "\n";
rmdir('./downloads/' . $content);
if(!empty($message)) {
mail($email, $found_subject, $message);

When the process encounters a difference of 30 days or more, it calls a user function (removeResource) that I reaped (among other pieces of this script) from the web that removes the download and the directory structure under the downloads directory that held them. (I don’t know where I got the function from and google doesn’t retrieve it. If anyone knows where it is from, let me know and I will gladly give credit!)

function removeResource( $_target ) {

if( is_file($_target) ) {
echo $_target . ' (file)<br />';
if( is_writable($_target) ) {
if( @unlink($_target) ) {
return true;
} else {
mail($email, $failed_subject, $message);
die("File delete unsuccessful. $message\n");

return false;

if( is_dir($_target) ) {
echo $_target . ' (dir)<br />';
if( is_writeable($_target) ) {
foreach( new DirectoryIterator($_target) as $_res ) {
if( $_res->isDot() ) {

if( $_res->isFile() ) {
removeResource( $_res->getPathName() );
} elseif( $_res->isDir() ) {
removeResource( $_res->getRealPath() );


if( @rmdir($_target) ) {
return true;
} else {
die("Directory delete unsuccessful.");
} else {
die("$_target is not writable.");

return false;

That concludes my tutorial on working with dates using PHP.

Come and check out my website found at

Thanks for taking the time out of your day to read my post!
Bud Manz

Leave a Comment

Scroll to Top