note 31502 deleted from function.mktime by sniper

From: Date: Tue, 04 Nov 2003 13:10:39 +0000
Subject: note 31502 deleted from function.mktime by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-59872@lists.php.net to get a copy of this message
Note Submitter: paul@santasoft.com ---- To recursively create directories for some reason or other, don't forget the strftime function when using mktime(0,0,0,$month,0,$year) or you'll end up making a million directories because mktime returns a full time number, not the last day of the month like you'd hope. Check out this example to perform such a task. ------ for ($year = 2001;$year <= 2004; $year++) { if (!is_dir("$year")) mkdir("$year",0777); for ($i=1; $i < 13; $i++) { if ($i < 12) { $mon = $i+1; $yr = $year; } else { $mon = 1; $yr = $year + 1; } $last_day = strftime ("%d", mktime (0,0,0,$mon,0,$yr)); if ($i < 10) $month = "0$i"; else $month = "$i"; if (!is_dir("$year/$month")) mkdir("$year/$month",0777); for ($j=1; $j <= $last_day; $j++) { if ($j < 10) $day = "0$j"; else $day = "$j"; if (!is_dir("$year/$month/$day")) mkdir("$year/$month/$day",0777); $d++; } } } echo("$d directories created"); ------ displays: 1461 directories created 1461 directories = 3 years x 365 days + 1 year x 366 days (2004) In the above example if you had forgotten the strftime portion of the code and just used the mktime, you would end up creating around 1075525200 directories or so. Most likely just kill the system. :) Did that about 4 times before I finally found that glitch in the code. The above code also shows an important example about getting the $last_day of a month in that you have to count for December being 12, but 13 as a month is invalid and won't work, but 1 (jan) of the next year does count. Fun Fun!

« previous php.notes (#59872) next »