Ecco quello che ho finora:Come calcolare la differenza tra due giorni come una stringa formattata?
/**
* Parse a duration between 2 date/times in seconds
* and to convert that duration into a formatted string
*
* @param integer $time_start start time in seconds
* @param integer $time_end end time in seconds
* @param string $format like the php strftime formatting uses %y %m %w %d %h or %i.
* @param boolean $chop chop off sections that have 0 values
*/
public static function FormatDateDiff($time_start = 0, $time_end = 0, $format = "%s", $chop = false) {
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
list($year_start,$month_start,$day_start) = explode('-',date('Y-m-d',$time_start));
list($year_end,$month_end,$day_end) = explode('-',date('Y-m-d',$time_end));
$years = $year_end - $year_start;
$months = $month_end - $month_start;
$days = $day_start - $day_end;
$weeks = 0;
$hours = 0;
$mins = 0;
$secs = 0;
if(mktime(0,0,0,$month_end,$day_end) < mktime(0,0,0,$month_start,$day_start)) {
$years -= 1;
}
if($days < 0) {
$months -= 1;
$days += 30; // this is an approximation...not sure how to figure this out
}
if($months < 0) $months += 12;
if(strpos($format, '%y')===false) {
$months += $years * 12;
}
if(strpos($format, '%w')!==false) {
$weeks = floor($days/7);
$days %= 7;
}
echo date('Y-m-d',$time_start).' to '.date('Y-m-d',$time_end).": {$years}y {$months}m {$weeks}w {$days}d<br/>";
}
(E 'incompleta e imprecisa)
io non riesco a ottenere la matematica destra. Dividerlo in modo creativo non funzionerà a causa degli anni bisestili e delle lunghezze di mesi diverse.
La logica deve anche cambiare in base alla stringa di formato. Ad esempio, passando da 04-Feb-2010 a 28-giu-2011 (come timestamp unix) con la stringa di formato %y year %m month %d day
dovrebbe essere emesso 1 year 4 month 24 day
ma se %y year
viene omesso, è necessario aggiungere 12 mesi al mese, ad esempio, l'output deve essere 16 month 24 day
.
Dovrebbe gestire anche i tempi ... ma non ce l'ho ancora.
Nessuna di queste soluzioni date_diff gestire settimane. E non so come potrei modificarlo in date_diff
, quindi non è davvero una soluzione per me.
Inoltre, $diff->format
non fa ciò che ho chiesto ... per indicare i mesi ei giorni totali se "unità più grandi" sono omesse. Esempio:
>>> $start = new DateTime('04-Feb-2010')
>>> $end = new DateTime('28-Jun-2011')
>>> $diff = $start->diff($end)
>>> $diff->format('%m months, %d days')
'4 months, 24 days'
Dovrebbe essere 16 months, 24 days
, come ho detto in precedenza. Per favore, smettila di essere così veloce da chiudere la mia domanda come un inganno prima di comprenderlo completamente. Se le soluzioni ad altre domande possono essere ottimizzate per risolvere questo, bene, ma per favore spiega come, perché non capisco.
Per essere chiari,
- se
%y
è omesso, anni dovrebbero essere rotolati nei mesi - se
%m
viene omesso, mesi dovrebbero essere rotolato in giorni - se
%w
è omesso, settimane deve essere inserito nei giorni - se si omette
%h
, le ore devono essere riportate in minuti - se
%m
è omesso, i minuti devono essere inseriti in secondi
Se "unità più piccole" vengono omesse, l'unità successiva più grande può essere arrotondata o pavimentata dove ha senso.
Se si dispone di PHP 5.3.0 o superiore, questo potrebbe aiutare a: [DateInterval :: format] (http://www.php.net/manual/en/dateinterval.format.php) – drew010
duplicato di [questo post] (http: // StackOverflow.it/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – bowlerae
@ drew010: No ... non gestisce le settimane. – mpen