PDA

View Full Version : Date Issue In PHP...



tomyknoker
05-06-2007, 11:33 AM
Can someone explain why this is happening... here is part of the code...


$date = date('d/m/Y');
$p = explode ( ' ', date('m Y') );

if ($p[0] == 1) {
$Month = 12;
$theYear = $p[1]-1;
}

else {
$Month = $p[0]-1;
$theYear = $p[1];
}

if ($Month < 10) {
$theMonth = '0'.$Month;
}

else {
$theMonth = $Month;
}

$monthText = date("F", mktime(0,0,0,$theMonth+1,0,0));

Basically from this I am pulling info out of my database about my members, this one pulls information of them logging from last month... It works fine, but I added in the query so it says 'These members logged in $monthText'... When I ran it it was saying 'March' so I had to add the +1 after $theMonth, just not sure why I need to add the +1, any ideas?

mwinter
05-06-2007, 07:27 PM
$monthText = date("F", mktime(0,0,0,$theMonth+1,0,0));


By specifying the date as zero (0) - an out-of-range value - the calculated date is rolled back one day into the preceding month.

If your main objective is to obtain the month as a string, I would have thought it would be computationally less expensive to use:



$monthNames = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$date = getdate();

$monthText = $monthNames[($date['mon'] + 10) % 12];

or at the very least:



$date = getDate();

$monthText = date('F', mktime(0, 0, 0, $date['mon'] - 1));

Mike