Phillip Mclauren
Phillip Mclauren

Reputation: 417

mktime function in php not working properly

I am using code:

$old_month = strtolower(date("m", mktime(date('H'), date('i'), date('s'), (date('m')-12),date('d'), date('Y'))));
$old_year = strtolower(date("Y", mktime(date('H'), date('i'), date('s'), (date('m')-12), date('d'), date('Y'))));


if ( $profile_stats['month'] < $old_month && $profile_stats['year'] == $old_year
|| $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y') ) 

The If loop should take value of the last month..i.e. Its october now..so If should search for sept ie. 9

How can I achieve that.

Please suggest any solution.

Upvotes: 1

Views: 358

Answers (2)

StaticVariable
StaticVariable

Reputation: 5283

Use this

$old_month = strtolower(date("m", strtotime("lastmonth"));


if ( $profile_stats['month'] < $old_month && $profile_stats['year'] == $old_year
|| $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y') ) 

should be

if ( strtotime($profile_stats['month']) < strtotime($old_month) && strtotime($profile_stats['year']) == strtotime($old_year)
|| $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y') ) 

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

You appear to be subtracting 12 from the number of months. That would make it last year.

Try using English - strtotime is surprisingly good at handling that:

$last_month = strtotime("-1 month");
list($old_month,$old_year) = explode("-",date("m-Y",$last_month));

Upvotes: 0

Related Questions