Something I often want to do is find out the first of the next month and to get the time() result for it. By using a combination of date() and strtotime(), you can do that:
<?php
// Example Time() Integer (for Thu, 15 Mar, 2012)
$time_to_handle_for_next_month_conversion = 1331835943;
// Date Conversions
$interpretted_day = date('d', $time_to_handle_for_next_month_conversion);
$interpretted_month = date('F', $time_to_handle_for_next_month_conversion);
$interpretted_month_lowercased = strtolower($interpretted_month);
$interpretted_year = date('Y', $time_to_handle_for_next_month_conversion);
// Preset Day
$new_day = 1;
// Handle Case: December
if($interpretted_month_lowercased == "december")
{
$new_month = "January";
$new_year = $interpretted_year + 1;
}
else
{
// Handle All Other Cases
switch($interpretted_month_lowercased)
{
case 'january':
$new_month = "February";
break;
case 'february':
$new_month = "March";
break;
case 'march':
$new_month = "April";
break;
case 'april':
$new_month = "May";
break;
case 'may':
$new_month = "June";
break;
case 'june':
$new_month = "July";
break;
case 'july':
$new_month = "August";
break;
case 'august':
$new_month = "September";
break;
case 'september':
$new_month = "October";
break;
case 'october':
$new_month = "November";
break;
case 'november':
$new_month = "December";
break;
}
$new_year = $interpretted_year;
}
$new_date_string = $new_day . " " .
$new_month . " " .
$new_year;
// Results
$first_day_of_the_next_month_time_integer = strtotime($new_date_string);
// Result = 1333256400 (Sun, 1 Apr, 2012)
?>
Note: You could easily modify this function to return the "next Ramadan," "the next Halloween," "the next New Years," etc., and place a counter for how many more days, months, hours, etc., you need to wait for it.
Official Function Page: http://php.net/manual/en/function.time.php
// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted. http://www.hacktivismo.com/about/hessla.php
No comments:
Post a Comment