CodeIgniter Forums
Previous month name - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Previous month name (/thread-5675.html)



Previous month name - El Forum - 01-31-2008

[eluser]Sawariya[/eluser]
hi friends how to get previous month name according to server date...??
anybody can help me??
thanks in advance


Previous month name - El Forum - 01-31-2008

[eluser]Lone[/eluser]
All you need to know on this is at the following link PHP.net Date Function


Previous month name - El Forum - 01-31-2008

[eluser]ejangi[/eluser]
You could do something to the effect of:
Code:
$month_name = date('F', (time()-(60*60*24*30)));

It's a bit hack-ish, but it should work and may spark a better solution.


Previous month name - El Forum - 01-31-2008

[eluser]Lone[/eluser]
Ok, you got me thinking on this and its not that easy to achieve in one line really because if month is January then current month less one is 0 not 12.

ucantblamem's solution is a good quick fix but the issue is obvious that not all months have 30 days...

Heres the best solution I could come up with - any suggestions welcome (such as something cleaner then mktime?).

Code:
if( ($monthPrevious = 1-1) == 0) {
    $monthPrevious = 12;
}

$monthPreviousName =  date('F',mktime(0,0,0,$monthPrevious,1,2000));

A previous date function in the date helper could come handy looking at this Smile


Previous month name - El Forum - 01-31-2008

[eluser]xwero[/eluser]
how about
Code:
date('F',strtotime("-1 month"));



Previous month name - El Forum - 01-31-2008

[eluser]ejangi[/eluser]
Lol, good man - short and to the point, I like it! Big Grin


Previous month name - El Forum - 01-31-2008

[eluser]Lone[/eluser]
xwero wins!

strtotime is such a magical function at times Tongue