I have two text input field. Each input field hold a date. By default the first field hold every time the current date, and second field hold te current date + 1 month.
this could help you, it's not that clean, because it defines a month as 31 days, but maybe that's enough for you:
PHP Code:
function diff_in_range($date1,$date2,$range=1,$seperator="-") {
//split parts of the date. IMPORTANT: dd mm yyyy order (seperator is an argument)
$date1 = explode($seperator,$date1);
$date2 = explode($seperator,$date2);
//create UNIX-Time (seconds since the epoche)
$unix_date1 = mktime(0,0,0,$date1[1],$date1[0],$date1[2]);
$unix_date2 = mktime(0,0,0,$date2[1],$date2[0],$date2[2]);
//If it's not too important, let's define a month as 31 days (60*60*24*31 = 2595600), this is not always true (e.g. february)
if ($unix_diff < (2595600*$range)) {
return true;
} else {
return false;
}
}
$date1 = "31-11-2006";
$date2 = "30-12-2006";
//check if there are more than 2 month (i.e. 62 days) between the dates
$result = diff_in_range($date1,$date2,2);
I don't understand this 30-60 days issue, but to get the difference in days between two dates, you can use a modified version of my function above:
PHP Code:
function diff_days($date1,$date2,$seperator="-") {
//split parts of the date. IMPORTANT: dd mm yyyy order (seperator is an argument)
$date1 = explode($seperator,$date1);
$date2 = explode($seperator,$date2);
//create UNIX-Time (seconds since the epoche)
$unix_date1 = mktime(0,0,0,$date1[1],$date1[0],$date1[2]);
$unix_date2 = mktime(0,0,0,$date2[1],$date2[0],$date2[2]);
If date2 is smaller than date1 I need to show an error. Your function know to do this ?
I want to use this feature to publish an article, and user need to specify the start date (date1) when article appear and end date (date2) when article expire.