To retrieve the month and day of the newest post on your blog via PHP (which you could of course include in a function) is relatively simple – in essence all that you are doing is putting in a call for the WordPress engine to return to you the newest post in the database and then extract the relevant information from there.
The code is as follows:
//Default values for the resulting variables
$newestyear = date('Y');
$newestmonth = date('m');
//Calculate newest Post Date
$args = array('orderby'=>'date','order'=>'DESC','posts_per_page'=>1,'caller_get_posts'=>1);
$newestpost = get_posts($args);
//Extract the date information
if (!empty($newestpost)){
$newestyear = mysql2date('Y',$newestpost[0]->post_date);
$newestmonth = mysql2date('m',$newestpost[0]->post_date);
}
//Print out the result
echo "
The newest post of this blog appeared in the $newestmonth month of $newestyear
To retrieve the month and day of the oldest post on your blog via PHP (which you could of course include in a function) is relatively simple – in essence all that you are doing is putting in a call for the WordPress engine to return to you the oldest post in the database and then extract the relevant information from there.
The code is as follows:
//Default values for the resulting variables
$oldestyear = date('Y');
$oldestmonth = date('m');
//Calculate Oldest Post Date
$args = array('orderby'=>'date','order'=>'ASC','posts_per_page'=>1,'caller_get_posts'=>1);
$oldestpost = get_posts($args);
//Extract the date information
if (!empty($oldestpost)){
$oldestyear = mysql2date('Y',$oldestpost[0]->post_date);
$oldestmonth = mysql2date('m',$oldestpost[0]->post_date);
}
//Print out the result
echo "
The oldest post of this blog appeared in the $oldestmonth month of $oldestyear
A simple way to calculate how many days are in a month in Javascript is to leverage Javascript’s built in date overflow feature – basically if you give it an incorrect date it automatically adjusts it to the correct one by assuming that the overflow are days which need simply to be added to the given date.
So, given a month and a year, it is a simply matter of forcing a date to overflow by a certain amount and then by subtracting the result from that overflow amount to learn how many days are in that month.
For example, to calculate the number of days in the current month:
var time=new Date();
var numberOfDays = 32 - new Date(time.getFullYear (), time.getMonth(), 32).getDate();
A simple way to calculate how many days are in a month in Javascript is to leverage Javascript’s built in date overflow feature – basically if you give it an incorrect date it automatically adjusts it to the correct one by assuming that the overflow are days which need simply to be added to the given date.
So, given a month and a year, it is a simply matter of forcing a date to overflow by a certain amount and then by subtracting the result from that overflow amount to learn how many days are in that month.
Thus by setting the start marker to one month back in time, we can easily learn how many days were in the last month, making it a snap to provide a quick last month range pick for a calendar control.
var time=new Date();
//force the date object to last month
var currentMonth = time.getMonth();
time.setMonth(time.getMonth()-1);
while(currentMonth == time.getMonth()){
time.setMonth(time.getMonth()-1);
}
//get the last day of last month
var lastDay = 32 - new Date(time.getFullYear (), time.getMonth(), 32).getDate();
//if we were setting a last month range for a calendar control
var monthstart = new Date(time.getFullYear (),time.getMonth(),1);
var monthend = new Date(time.getFullYear (),time.getMonth(),lastDay);
Wow, stumbled across this extremely clever, one line of PHP code to return the correct number of days for any given month, courtesy of the extremely talented David Walsh.
Now while PHP does have an inbuilt function to handle this query (cal_days_in_month), it isn’t always the most ideal of saviors when you consider that it is only valid for PHP 4.0.7 and above installations. David’s on the other hand simply makes use of good old logic and thus pretty much roles out to just about any version of PHP.
And the function? Well, it’s a simple enough call that requires only the target month and year as inputs and looks like this: