blurredvision Posted June 1, 2009 Share Posted June 1, 2009 I'm needing to create links to bring back the previous month and year in just their numbers. I think it has to be done using something like mktime(), but I'm not at all familiar with it. I'd like the cleanest possible solution, so that's why I'm asking you guys for help. Today is June 1st, 2009. Or, 6-2009. I'd like a quick variable that will bring me 5-2009 (ideally, the month number and year would be separate variables). So in my perfect world, I'd end up with $prev_month = 5; and $year = 2009;. If today were January 15th, 2009, I'd want to end up with $prev_month = 12; and $year = 2008;. Pretty simple, but I don't have any idea how to do it. Do I use mktime(), or can I do some kind of date('n') -1;? I appreciate the help and learning! Link to comment https://forums.phpfreaks.com/topic/160559-solved-how-do-i-bring-back-the-previous-number-of-month-and-year/ Share on other sites More sharing options...
ldougherty Posted June 2, 2009 Share Posted June 2, 2009 Here ya go.. $lastmonth = date('m-Y',mktime(0, 0, 0, date("m")-1, date("d"), date("Y"))); echo "last month is $lastmonth"; Link to comment https://forums.phpfreaks.com/topic/160559-solved-how-do-i-bring-back-the-previous-number-of-month-and-year/#findComment-847394 Share on other sites More sharing options...
kenrbnsn Posted June 2, 2009 Share Posted June 2, 2009 Another way: <?php list($prev_year,$prev_month) = explode('-',date('Y-m',strtotime('-1 month'))); echo 'Previous month-year: ' . $prev_month . ' - ' . $prev_year; ?> Ken Link to comment https://forums.phpfreaks.com/topic/160559-solved-how-do-i-bring-back-the-previous-number-of-month-and-year/#findComment-847406 Share on other sites More sharing options...
blurredvision Posted June 2, 2009 Author Share Posted June 2, 2009 Thanks guys. I used Ken's script to great success, and I'll dissect it to implement it into my knowledge. Link to comment https://forums.phpfreaks.com/topic/160559-solved-how-do-i-bring-back-the-previous-number-of-month-and-year/#findComment-847435 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.