Day count between two dates
I want to be able to calculate the amount of days between two dates.
I have a drop down menu which selects a day (1-31), a month (1-12) and a year (2011-2012) and then it is posted in to a database in the format 1122011 (1st December 2011).
I want to get that variable with a date from the database ($lastpost) which would be displayed as simply, "7102011" (7th October 2011) and calculate the amount of days between the date in the variable $lastpost and "now". I know it would need to be formatted differently fo开发者_开发问答r it to work, but I'm unsure how to do it.
I would convert each date to a unix timestamp.
http://www.php.net/manual/en/function.mktime.php
Then get the difference between the two. endDate-startDate
Then just do some math to figure out the days from seconds.
If you are storing dates in MySQL, you could use the built-in DATE type.
Then you could just use the also built-in DATEDIFF function:
SELECT DATEDIFF(NOW(), your_date) FROM your_table
Gives you the days between today and the date in your_date.
I use this functio to calculate the time elapsed between now and $fecha, you could simply add another parameter with $fecha2 (date 2) instead of $ahora (now)
function hace($fecha){
//obtener la hora en formato unix
$ahora=time()-3600;
$fecha_unix = strtotime($fecha);
// mostrar_notificacion($fecha_unix);
//obtener la diferencia de segundos;
$segundos=$ahora-$fecha_unix+1;
//dias es la division de n segs entre 86400 segundos que representa un dia;
$dias=floor($segundos/86400);
//mod_hora es el sobrante, en horas, de la division de días;
$mod_hora=$segundos%86400;
//hora es la division entre el sobrante de horas y 3600 segundos que representa una hora;
$horas=floor($mod_hora/3600);
//mod_minuto es el sobrante, en minutos, de la division de horas;
$mod_minuto=$mod_hora%3600;
//minuto es la division entre el sobrante y 60 segundos que representa un minuto;
$minutos=floor($mod_minuto/60)+1;
// this is only to print the result;
if($horas<=0){
if($minutos <= 1){
return get_texto_clave('hace') ." ".$segundos." ".get_texto_clave('segundos')." " .get_texto_clave('a_go');
}else{
return get_texto_clave('hace') ." ".$minutos." ".get_texto_clave('minutes')." " .get_texto_clave('a_go');
}
}elseif($dias<=0){
return get_texto_clave('hace') ." ".$horas." ".get_texto_clave('hours') ." ". get_texto_clave('and') ." ".$minutos." ".get_texto_clave('minutes');
}else{
return get_texto_clave('hace') ." ".$dias." ".get_texto_clave('dias');
}
}
It's common to have dates in ISO 8601 notation Wikipedia like YYYY-MM-DD
, e.g. 2011-10-07
for 7th October 2011. Such a format is well structured and PHP as well as MySQL has support for.
This format is well structured and it's always clear where the month ends and the day begins.
You should in any case save date values as DATE
Type Docs into the MySQL database.
To convert your input into such a date-string you can do easily with sprintf
Docs:
$day = '7';
$month = '10';
$year = '2011';
$iso = sprintf('%02d-%02d-%04d', $day, $month, $year);
echo $iso; # 07-10-2011
MySQL can then read it as an actual date value and can do calculations based on it, like Ailyn outlined in his answer.
I managed to solve it using a simplish method using the strtotime
function, I'm not sure if I explined it very well to you all but this is my almost finished code.
$today = strtotime("2011-08-30 00:00:00");
$lastpost2 = strtotime($lastpost);
printf("%d", round(abs($today-$lastpost2)/60/60/24));
$today being the current date, which will probably use the NOW() function, and then $lastpost2 is getting the date from the database ($lastpost) and then then it's printed with the exact amount of days between the two dates below.
精彩评论