Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was trying to calculate the difference of two dates for the purpose of showing the duration of an event. Most of the functions given on the problem fails if the event has a duration form friday at 17:00 to sunday at 15:00. My goal was to find the difference between the dates like:</p> <pre><code>date('Ymd',$end)-date('Tmd',$begining); </code></pre> <p>But that is likly to fail because there isn't 99 month in a year and 99 days in a month. I could convert the date string to UNIX timestamp and divide by 60*60*12, but some days have a greater or lesser number of hours, sometimes there's eaven a leap secound. So I made my own function using getdate() a function that returns an array of innformation about the timestamp.</p> <pre><code>/* * datediff($first,$last) * $first - unix timestamp or string aksepted by strtotime() * $last - unix timestamp or string aksepted by strtotime() * * return - the difference in days betveen the two dates */ function datediff($first,$last){ $first = is_numeric($first) ? $first : strtotime($first); $last = is_numeric($last ) ? $last : strtotime($last ); if ($last&lt;$first){ // Can't calculate negative difference in dates return -1; } $first = getdate($first); $last = getdate($last ); // find the difference in days since the start of the year $datediff = $last['yday'] - $first['yday']; // if the years do not match add the appropriate number of days. $yearCountedFrom = $first['year']; while($last['year'] != $yearCountedFrom ){ // Take leap years into account if( $yearCountedFrom % 4 == 0 &amp;&amp; $yearCountedFrom != 1900 &amp;&amp; $yearCountedFrom != 2100 ){ //leap year $datediff += 366; }else{ $datediff += 365; } $yearCountedFrom++; } return $datediff; } </code></pre> <p>Concerning the GregorianToJD() function, it might work, but I feel a little bit uneasy since I do not understand how it work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload