Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 21
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Gratulacje i dzięki! Trafiłem tu przypadkiem poszukując informacji na temat php+mysql. Wiele polskich stron powiela identyczne przykłady, klonuje te same kursy i lekcje... ten serwis okazał sie inny. Zasada "problem - rozwiazanie - wyjaśnienie" zdaje egzamin - zapewnia jasną, jednoznaczną i pewną pomoc w konkretnym przypadku. Porady są warte swojej ceny, przede wszystkim ze względu na przyjazną (także dla początkujących) formę i treść oraz bogate i stale powiększane zasoby. Polecam i pozdrawiam!

Kamil Dmowski
Polski Czerwony Krzyż

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

XIX. Data i Czas

Wstęp

Funkcje opisane w tym rozdziale służą do obsługi daty i czasu. Pozwalają one na pobieranie aktualnej daty i czasu z serwera, na którym pracuje PHP. Posiadają one możliwości formatowania daty i czasu na wyjściu na wiele różnych sposobów.

Notatka: Proszę pamiętać, że poniższe funkcje są zależne od lokalnych ustawień danego serwera. Należy tu szczególnie wziąć pod uwagę czas letni/zimowy i lata przestępne.

Wymagania

Do zbudowania tego rozszerzenia nie są wymagane żadne zewnętrzne biblioteki.

Instalacja

By używać tych funkcji, nie trzeba niczego instalować. Są one częścią jądra PHP.

Konfiguracja czasu wykonywania

To rozszerzenie nie definiuje posiada żadnych dyrektyw konfiguracyjnych w pliku php.ini.

Typy zasobów

To rozszerzenie nie posiada żadnych rodzajów zasobów.

Stałe predefinopwane

To rozszerzenie nie posiada żadnych stałych.

Spis treści
checkdate -- Waliduje datę gregoriańską
date_sunrise --  Returns time of sunrise for a given day and location
date_sunset --  Returns time of sunset for a given day and location
date -- Formatuje lokalny czas/datę
getdate -- Pobiera informację o dacie/czasie
gettimeofday -- Pobiera aktualny czas
gmdate -- Formatuje datę/czas dla strefy GMT/CUT
gmmktime -- Ustala uniksowy znacznik czasu dla daty ze strefy GMT
gmstrftime --  Formatuje czas/datę ze strefy GMT/UTC zgodnie z lokalizacją
idate --  Format a local time/date as integer
localtime -- Pobiera czas lokalny
microtime --  Zwraca aktualny uniksowy znacznik czasu z mikrosekundami
mktime -- Oblicza uniksowy znacznik czasu dla podanej daty
strftime --  Formatuje lokalną datę/czas zgodnie z lokalizacją
strptime --  Parse a time/date generated with strftime()
strtotime --  Parsuje większość angielskich tekstowych opisów daty i czasu do uniksowego znacznika czasu
time -- Zwraca aktualny uniksowy znacznik czasu



User Contributed Notes

worm (zantATglazovDOTnet)
25-Jan-2006 03:44

Function for converting  RFC 2822 formatted date to timestamp
<?php

/**
 * @param  string  $date RFC 2822 formatted date
 * @return integer timestamp
 */

function Rfc2822ToTimestamp($date){
 
$aMonth = array(
            
"Jan"=>"1", "Feb"=>"2", "Mar"=>"3", "Apr"=>"4", "May"=>"5",
            
"Jun"=>"6", "Jul"=>"7", "Aug"=>"8", "Sep"=>"9", "Oct"=>"10",
            
"Nov"=>"11", "Dec"=>"12");

  list( ,
$day, $month, $year, $time) = explode(" ", $date);
  list(
$hour, $min, $sec) = explode(":", $time);
 
$month = $aMonth[$month];

  return
mktime($hour, $min, $sec, $month, $day, $year);
}
?>


Jeff
03-Nov-2005 04:29

Here is a somewhat simpler function for getting the number of  business days between two dates

<?php

function WorkDays( $startTime, $endTime )
{
  
$workdays = 0 ;
   while(
$startTime <= $endTime )
   {
       if(
date('w', $startTime ) != 6 && date( 'w', $startTime) != 0 )
       {
          
$workdays++ ;
       }
      
$startTime += 86400 ;
   }
   return
$workdays ;
}

?>


cupidomind at yahoo dot fr
15-Oct-2005 05:18

Hi I just want to say thanks a lot to the man who wrote a solution about a problem of the date-difference.

/*A much easier way to do days diff is to use Julian Days from the Calendar functions:

$start = gregoriantojd($smon, $sday, $syear);
$end = gregoriantojd($emon, $eday, $eyear);
$daysdiff = $end - $start;

You can see the obvious ways to wrap a function around that.*/


rycker+phpdate at gmail dot com
04-Oct-2005 09:30

Function for converting MySQL timestamp to Datetime format

function TimestampToDatetime($Tstamp) {
   $dt[0] = substr($Tstamp,0,4);
   $dt[1] = substr($Tstamp,4,2);
   $dt[2] = substr($Tstamp,6,2);
   $tm[0] = substr($Tstamp,8,2);
   $tm[1] = substr($Tstamp,10,2);
   $tm[2] = substr($Tstamp,12,2);
   return (join($dt,"-") . " " . join($tm,":"));
}


andreencinas at yahoo dot com dot br
28-Sep-2005 02:08

//function like dateDiff Microsoft
       //not error in year Bissesto

       function dateDiff($interval,$dateTimeBegin,$dateTimeEnd) {
         //Parse about any English textual datetime
         //$dateTimeBegin, $dateTimeEnd

         $dateTimeBegin=strtotime($dateTimeBegin);
         if($dateTimeBegin === -1) {
           return("..begin date Invalid");
         }

         $dateTimeEnd=strtotime($dateTimeEnd);
         if($dateTimeEnd === -1) {
           return("..end date Invalid");
         }

         $dif=$dateTimeEnd - $dateTimeBegin;

         switch($interval) {
           case "s"://seconds
               return($dif);

           case "n"://minutes
               return(floor($dif/60)); //60s=1m

           case "h"://hours
               return(floor($dif/3600)); //3600s=1h

           case "d"://days
               return(floor($dif/86400)); //86400s=1d

           case "ww"://Week
               return(floor($dif/604800)); //604800s=1week=1semana

           case "m": //similar result "m" dateDiff Microsoft
               $monthBegin=(date("Y",$dateTimeBegin)*12)+
                 date("n",$dateTimeBegin);
               $monthEnd=(date("Y",$dateTimeEnd)*12)+
                 date("n",$dateTimeEnd);
               $monthDiff=$monthEnd-$monthBegin;
               return($monthDiff);

           case "yyyy": //similar result "yyyy" dateDiff Microsoft
               return(date("Y",$dateTimeEnd) - date("Y",$dateTimeBegin));

           default:
               return(floor($dif/86400)); //86400s=1d
         }

       }


glashio at xs4all dot nl
27-Sep-2005 09:46

Calculate Sum BusinessDays (Mon till Fri) between two date's :

<?php
function businessdays($begin, $end) {
  
$rbegin = is_string($begin) ? strtotime(strval($begin)) : $begin;
  
$rend = is_string($end) ? strtotime(strval($end)) : $end;
   if (
$rbegin < 0 || $rend < 0)
       return
0;

  
$begin = workday($rbegin, TRUE);
  
$end = workday($rend, FALSE);

   if (
$end < $begin) {
      
$end = $begin;
      
$begin = $end;
   }

  
$difftime = $end - $begin;
  
$diffdays = floor($difftime / (24 * 60 * 60)) + 1;

   if (
$diffdays < 7) {
      
$abegin = getdate($rbegin);
      
$aend = getdate($rend);
       if (
$diffdays == 1 && ($astart['wday'] == 0 || $astart['wday'] == 6) && ($aend['wday'] == 0 || $aend['wday'] == 6))
           return
0;
      
$abegin = getdate($begin);
      
$aend = getdate($end);
      
$weekends = ($aend['wday'] < $abegin['wday']) ? 1 : 0;
   } else
      
$weekends = floor($diffdays / 7);
   return
$diffdays - ($weekends * 2);
}

function
workday($date, $begindate = TRUE) {
  
$adate = getdate($date);
  
$day = 24 * 60 * 60;
   if (
$adate['wday'] == 0) // Sunday
      
$date += $begindate ? $day : -($day * 2);
   elseif (
$adate['wday'] == 6) // Saterday
      
$date += $begindate ? $day * 2 : -$day;
   return
$date;
}
?>


Eric Z (ezsomething at hotmail)
01-Sep-2005 11:56

I was having a horrible time trying to get a good list of timezones, how to set them locally (for the user/client), and how best to keep this information. Building on the notes of this site (thanks everyone), I constructed code that should work on just about any un*x based platform.  It reads the local timezone table and gives your a nested array of the continents and regions of the file -- the natural continent sorting is even by the most populated areas (thanks Paul Eggert!).

Afterwards, all you have to do is export the timezone string with an environmental set.. which works just fine if php is running as an apache model; haven't tested it for commandline, but I suspect it's okay there, too.

<?php
if (isset($_SESSION[PROFILE_TZOFFSET]))
      
putenv('TZ='.$_SESSION[PROFILE_TZOFFSET]);
?>

Here's how to load the timezones... the nested array makes it easy to insert into html lists or other well-behaved objects.

<?php
  
function getTimezoneData() {
    
//EricZ - 9/1/05 - free to use, keep the comments :)
    
$zoneNames = array();

    
// -- first part, gather all of the zone data
    
$zoneSource = '/usr/share/zoneinfo/zone.tab';
    
$zoneHandle = fopen($zoneSource, "r");
     if (!
$zoneHandle) return NULL;                  //bad file, abort now
    
while (!feof($zoneHandle)) {
      
$zoneLine = ltrim(fgets($zoneHandle, 4096));
       if (
$zoneLine[0]=='#')  continue;          //skip comments
         //Columns...
         // 1.  ISO 3166 2-character country code.
         // 2.  Latitude and longitude of the zone's principal location
         // 3.  Zone name used in value of TZ environment variable.
         // 4.  Comments; present if and only if country has multiple rows.
      
$zoneParts = explode("\t",$zoneLine);      //grab parts
      
if (count($zoneParts) < 3) continue;      //erroneous line!
      
$nameParts = explode('/', $zoneParts[2], 2);  //grab country/name
      
$zoneKey = $nameParts[0];                  //country or area

      
$insertArray = &$zoneNames;                //where to insert?
      
if (count($nameParts) > 1) {                //area/populous
        
if (!isset($zoneNames[$zoneKey]))        //not set before
          
$zoneNames[$zoneKey] = array();
        
$insertArray = &$zoneNames[$zoneKey];    //grab sub (country)
        
$zoneKey = trim($nameParts[1]);          //grab correct key
      
}
      
$zoneKey = preg_replace('/[_]/',' ', $zoneKey);
      
$insertArray[$zoneKey] = trim($zoneParts[2]); //actually set data
    
}                                            //end while not eof
    
fclose($zoneHandle);
     return
$zoneNames;
   }
?>


daniel at globalnetstudios dot com
08-Jun-2005 08:49

This dateDiff() function can take in just about any timestamp, including UNIX timestamps and anything that is accepted by strtotime(). It returns an array with the ability to split the result a couple different ways. I built this function to suffice any datediff needs I had. Hope it helps others too.

<?php
 
/********* dateDiff() function **********
   * returns Array of Int values for difference between two dates
   * $date1 > $date2 --> positive integers are returned
   * $date1 < $date2 --> negative integers are returned
   *
   * $split recognizes the following:
   *  'yw' = splits up years, weeks and days (default)
   *  'y'  = splits up years and days
   *  'w'  = splits up weeks and days
   *  'd'  = total days
   *
   * examples:
   *  $dif1 = dateDiff() or dateDiff('yw')
   *  $dif2 = dateDiff('y')
   *  $dif3 = dateDiff('w')
   *  $dif4 = dateDiff('d')
   *
   * assuming dateDiff returned 853 days, the above
   * examples would have a print_r output of:
   *  $dif1 == Array( [y] => 2 [w] => 17 [d] => 4 )
   *  $dif2 == Array( [y] => 2 [d] => 123 )
   *  $dif3 == Array( [w] => 121 [d] => 6 )
   *  $dif4 == Array( [d] => 847 )
   *
   * note: [h] (hours), [m] (minutes), [s] (seconds) are always returned as elements of the Array
   */
 
function dateDiff($dt1, $dt2, $split='yw') {
  
$date1 = (strtotime($dt1) != -1) ? strtotime($dt1) : $dt1;
  
$date2 = (strtotime($dt2) != -1) ? strtotime($dt2) : $dt2;
  
$dtDiff = $date1 - $date2;
  
$totalDays = intval($dtDiff/(24*60*60));
  
$totalSecs = $dtDiff-($totalDays*24*60*60);
  
$dif['h'] = $h = intval($totalSecs/(60*60));
  
$dif['m'] = $m = intval(($totalSecs-($h*60*60))/60);
  
$dif['s'] = $totalSecs-($h*60*60)-($m*60);
  
// set up array as necessary
  
switch($split) {
   case
'yw': # split years-weeks-days
    
$dif['y'] = $y = intval($totalDays/365);
    
$dif['w'] = $w = intval(($totalDays-($y*365))/7);
    
$dif['d'] = $totalDays-($y*365)-($w*7);
     break;
   case
'y': # split years-days
    
$dif['y'] = $y = intval($totalDays/365);
    
$dif['d'] = $totalDays-($y*365);
     break;
   case
'w': # split weeks-days
    
$dif['w'] = $w = intval($totalDays/7);
    
$dif['d'] = $totalDays-($w*7);
     break;
   case
'd': # don't split -- total days
    
$dif['d'] = $totalDays;
     break;
   default:
     die(
"Error in dateDiff(). Unrecognized \$split parameter. Valid values are 'yw', 'y', 'w', 'd'. Default is 'yw'.");
   }
   return
$dif;
  }
?>


mail at completeideas dot com
06-Jun-2005 10:55

For those who are using pre MYSQL 4.1.1, you can use:

TO_DAYS([Date Value 1])-TO_DAYS([Date Value 2])

For the same result as:

DATEDIFF([Date Value 1],[Date Value 2])


r00t_ at mail dot ru
06-May-2005 09:33

Function generate one month calendar like

February
Mon Tue Wed Thu Fri Sat Sun
               1  2  3  4
   5  6  7    8  9    10  11
 12  13  14  15  16  17 18
 19  20  21  22  23  24 25
 26  27  28

(default in russian locale)

<?php

$year
= (int)$_GET["year"];
$month = (int)$_GET["month"];

draw_month_cal($year, $month);

function
draw_month_cal($year, $month, $locale = array ('ru_RU.CP1251', 'rus_RUS.1251'))
{
   if (
checkdate($month, 1, $year) && setlocale (LC_TIME, $locale)) {
  
       if (!
$day = date("w", $f_day = mktime(0, 0, 0, $month, 1, $year)))
          
$day = 7; // Mon first, Sun last
      
      
print "<table border=0><tr><th colspan=7>" . strftime("%B", $f_day) . "</td></tr><tr>"; // Month
      
      
for ($i = 8; --$i;)
           print
"<th>" . strftime("%a", mktime(0, 0, 0, $month, 16 - $i - $day, $year)) . "</th>"; // Mon Tue Wed Thu Fri Sat Sun 
          
      
print "</tr><tr>" . str_repeat("<td></td>", --$day); // Empty cells
      
      
while (checkdate($month, ++$i, $year)) { // $i==0 after for :-)
          
print "<td>$i</td>";
           if (!(++
$day % 7)) print "</tr><tr>"; // next line after Sun
      
}
       print
"</tr></table>";
   }
}

?>


info at programare dot org
05-May-2005 07:34

A simple DateAdd() function:

function DateAdd($v,$d=null , $f="d/m/Y"){
  $d=($d?$d:date("Y-m-d"));
  return date($f,strtotime($v." days",strtotime($d)));
}

Then use it:

echo DateAdd(2);  // 2 days after
echo DateAdd(-2,0,"Y-m-d");  // 2 days before with gigen format
echo DateAdd(3,"01/01/2000");  // 3 days after given date


datavortex at gmail dot com
19-Mar-2005 12:19

This is a litttle function I cobbled together from my own code, and snippets from this site to calculate the difference between two datetimes without having to confine it to simply one interval.  This will tell you how many weeks, days, hours, minutes, and seconds there are between the given datetimes, and also makes a little English string you can use.

This could easily be expanded to include months, and years, but I didn't want to have to deal with any of the leap year and variable month length stuff.

<?
function dateDiff($dateTimeBegin,$dateTimeEnd) {
 
$dateTimeBegin =strtotime($dateTimeBegin);
 
$dateTimeEnd  =strtotime($dateTimeEnd);

  if(
$dateTimeEnd === -1 || $dateTimeBegin === -1) {
  
# error condition
  
return false;
  }

 
$diff = $dateTimeEnd - $dateTimeBegin;

  if (
$diff < 0) {
  
# error condition
  
return false;
  }

 
$weeks = $days = $hours = $minutes = $seconds = 0; # initialize vars

 
if($diff % 604800 > 0) {
  
$rest1  = $diff % 604800;
  
$weeks  = ($diff - $rest1) / 604800; # seconds a week
  
if($rest1 % 86400 > 0) {
    
$rest2 = ($rest1 % 86400);
    
$days  = ($rest1 - $rest2) / 86400; # seconds a day
    
if( $rest2 % 3600 > 0 ) {
      
$rest3 = ($rest2 % 3600);
      
$hours = ($rest2 - $rest3) / 3600; # seconds an hour
      
if( $rest3 % 60 > 0 ) {
        
$seconds = ($rest3 % 60);
        
$minutes = ($rest3 - $seconds) / 60# seconds a minute
      
} else {
        
$minutes = $rest3 / 60;
       }
     } else {
      
$hours = $rest2 / 3600;
     }
   } else {
    
$days = $rest1/ 86400;
   }
  }else {
  
$weeks = $diff / 604800;
  }

 
$string = array();
  if(
$weeks > 1) {
  
$string[]  = "$weeks weeks";
  } elseif (
$weeks == 1) {
  
$string[]  = "a week";
  }
  if(
$days > 1) {
  
$string[] = "$days days";
  } elseif(
$days == 1) {
  
$string[] = "a day";
  }
  if(
$hours > 1) {
  
$string[] = "$hours hours";
  } elseif (
$hours == 1) {
  
$string[] = "an hour";
  }
  if(
$minutes > 1) {
  
$string[] = "$minutes minutes";
  } elseif (
$minutes == 1) {
  
$string[] = "a minute";
  }
  if(
$seconds > 1) {
  
$string[] = "$seconds seconds";
  } elseif(
$seconds == 1) {
  
$string[] = "a second";
  }

 
# join together all the strings in the array above except the last element
 
$text  = join(', ', array_slice($string,0,sizeof($string)-1)) . ", and ";
 
$text .= array_pop($string);  # put the last one on after the and
 
 
return array($text, $weeks, $days, $hours, $minutes, $seconds);
?>


JMPZ art JMPZ dort ORG
04-Mar-2005 05:31

If you are dealing with a date in a database, you could just use the mysql function DATEDIFF(expr1,expr2) To calculate the difference without big bulky php functions.


andreencinas at yahoo dot com dot br
18-Jan-2005 05:56

//function like dateDiff Microsoft
       //bug update for previous

       function dateDiff($interval,$dateTimeBegin,$dateTimeEnd) {
         //Parse about any English textual datetime
         //$dateTimeBegin, $dateTimeEnd

         $dateTimeBegin=strtotime($dateTimeBegin);
         if($dateTimeBegin === -1) {
           return("..begin date Invalid");
         }

         $dateTimeEnd=strtotime($dateTimeEnd);
         if($dateTimeEnd === -1) {
           return("..end date Invalid");
         }

         $dif=$dateTimeEnd - $dateTimeBegin;

         switch($interval) {
           case "s"://seconds
               return($dif);

           case "n"://minutes
               return(floor($dif/60)); //60s=1m

           case "h"://hours
               return(floor($dif/3600)); //3600s=1h

           case "d"://days
               return(floor($dif/86400)); //86400s=1d

           case "ww"://Week
               return(floor($dif/604800)); //604800s=1week=1semana

           case "m": //similar result "m" dateDiff Microsoft
               $monthBegin=(date("Y",$dateTimeBegin)*12)+
                 date("n",$dateTimeBegin);
               $monthEnd=(date("Y",$dateTimeEnd)*12)+
                 date("n",$dateTimeEnd);
               $monthDiff=$monthEnd-$monthBegin;
               return($monthDiff);

           case "yyyy": //similar result "yyyy" dateDiff Microsoft
               return(date("Y",$dateTimeEnd) - date("Y",$dateTimeBegin));

           default:
               return(floor($dif/86400)); //86400s=1d
         }

       }


Elizalde Baguinon
08-Jan-2005 03:46

I evaluated the modified version of Xiven's datediff (below) and I saw some errors. I switched the lines of getting the seconds and the formatting of date. I was testing the datediff() function with a "d" interval. Here I added my test code.

$dateA = "2004-12-31";
$dateB = "2005-01-01";

function datediff($interval, $date1, $date2) {
   // Function roughly equivalent to the ASP "DateDiff" function
 
   /* Get the seconds first */
   $seconds = strtotime($date2) - strtotime($date1);

   $date1=date("Y-m-d", strtotime($date1));
   $date2=date("Y-m-d",strtotime($date2));
 
   switch($interval) {
       case "y":
           list($year1, $month1, $day1) = split('-', $date1);
           list($year2, $month2, $day2) = split('-', $date2);
           $time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
           $time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
           $diff = $year2 - $year1;
           if($month1 > $month2) {
               $diff -= 1;
           } elseif($month1 == $month2) {
               if($day1 > $day2) {
                   $diff -= 1;
               } elseif($day1 == $day2) {
                   if($time1 > $time2) {
                       $diff -= 1;
                   }
               }
           }
           break;
       case "m":
           /* parses the year, month and days. split() was replaced with explode(), PHP Manual says it's faster */
           list($year1, $month1, $day1) = explode('-', $date1);
           list($year2, $month2, $day2) = explode('-',$date2);

           $time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
           $time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
          
           $diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1);

           if($day1 > $day2) {
               $diff -= 1;
           } elseif($day1 == $day2) {
               if($time1 > $time2) {
                   $diff -= 1;
               }
           }
           break;
       case "w":
           // Only simple seconds calculation needed from here on
           $diff = floor($seconds / 604800);
           break;
       case "d":
           $diff = floor($seconds / 86400);
           break;
       case "h":
           $diff = floor($seconds / 3600);
           break;     
       case "i":
           $diff = floor($seconds / 60);
           break;     
       case "s":
           $diff = $seconds;
           break;     
   }
   //return the +ve integer only
   if ($diff<0){
     $diff=0-$diff;
   }
   return $diff;
}

echo "x: $dateA<br>";
echo "y: $dateB<br>";
echo "<br>";

echo datediff ("d",$dateA, $dateB);


Ruby
07-Jan-2005 05:09

just modified from Xiven

function datediff($interval, $date1, $date2) {
   // Function roughly equivalent to the ASP "DateDiff" function
  
   //set the date format first
   $date1= date("Y-m-d", strtotime($date1));
   $date2= date("Y-m-d",strtotime($date2));
  
   $seconds = $date2 - $date1;
  
   switch($interval) {
       case "y":
           list($year1, $month1, $day1) = split('-', $date1);
           list($year2, $month2, $day2) = split('-', $date2);
           $time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
           $time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
           $diff = $year2 - $year1;
           if($month1 > $month2) {
               $diff -= 1;
           } elseif($month1 == $month2) {
               if($day1 > $day2) {
                   $diff -= 1;
               } elseif($day1 == $day2) {
                   if($time1 > $time2) {
                       $diff -= 1;
                   }
               }
           }
           break;
       case "m":
           list($year1, $month1, $day1) = split('-', $date1);
           list($year2, $month2, $day2) = split('-',$date2);
           $time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
           $time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
           $diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1);
           if($day1 > $day2) {
               $diff -= 1;
           } elseif($day1 == $day2) {
               if($time1 > $time2) {
                   $diff -= 1;
               }
           }
           break;
       case "w":
           // Only simple seconds calculation needed from here on
           $diff = floor($seconds / 604800);
           break;
       case "d":
           $diff = floor($seconds / 86400);
           break;
       case "h":
           $diff = floor($seconds / 3600);
           break;       
       case "i":
           $diff = floor($seconds / 60);
           break;       
       case "s":
           $diff = $seconds;
           break;       
   }
   //return the +ve integer only
   if ($diff <0)
   {
           $diff= 0-$diff;
   }
   return $diff;
}


mincklerstraat at softhome dot net
11-Oct-2004 10:43

Before you get too advanced using date functions, be sure also to see the calendar functions at http://www.php.net/manual/en/ref.calendar.php .


charles at etherscapes dot com
04-Jun-2004 01:54

There are two dates that I know of that produce an incorrect result for the date functions above: 2004-04-04 and 2004-04-05. The days difference is zero instead of one.


jens at jebecs dot de
03-Jun-2004 01:29

There is an error in vincentv's post from 07-Feb-2001 11:23:
In function dayDiff(..) the return statement must be replaced by:
<?
return ( (getYear($timestamp1)*365 + $dayInYear1) -
         (
getYear($timestamp2)*365 + $dayInYear2) );
?>


nickaubert at america's biggest isp dot com
12-Apr-2004 10:13

I ran into an issue using a function that loops through an array of dates where the keys to the array are the Unix timestamp for midnight for each date.  The loop starts at the first timestamp, then incremented by adding 86400 seconds (ie. 60 x 60 x 24).  However, Daylight Saving Time threw off the accuracy of this loop, since certain days have a duration other than 86400 seconds.  I worked around it by adding a couple of lines to force the timestamp to midnight at each interval.

<?php
  $ONE_DAY
= 90000// can't use 86400 because some days have one hour more or less
 
for ( $each_timestamp = $start_time ; $each_timestamp <= $end_time ; $each_timestamp +=  $ONE_DAY) {

  
/*  force midnight to compensate for daylight saving time  */
  
$this_timestamp_array = getdate( $each_timestamp );
  
$each_timestamp = mktime ( 0 , 0 , 0 , $this_timestamp_array[mon] , $this_timestamp_array[mday] , $this_timestamp_array[year] );

    
// do some stuff...
 
}
?>


pk_jsp at rediffmail dot com
12-Apr-2004 09:06

Just want to add a comment to the function datediff given by Xiven that simple difference of 2 dates as in
 $seconds = $date2 - $date1; will nor work instead the following need to be used.
 $seconds = strtotime($date2) - strtotime($date1);


scott_webster_2000 at yahoo dot com
20-Feb-2004 07:02

Here is a slight improvement over wwb_99@yahoo's entry.  (It works now.)

function date_diff($earlierDate, $laterDate) {
  //returns an array of numeric values representing days, hours, minutes & seconds respectively
  $ret=array('days'=>0,'hours'=>0,'minutes'=>0,'seconds'=>0);

  $totalsec = $laterDate - $earlierDate;
  if ($totalsec >= 86400) {
   $ret['days'] = floor($totalsec/86400);
   $totalsec = $totalsec % 86400;
  }
  if ($totalsec >= 3600) {
   $ret['hours'] = floor($totalsec/3600);
   $totalsec = $totalsec % 3600;
  }
  if ($totalsec >= 60) {
   $ret['minutes'] = floor($totalsec/60);
  }
  $ret['seconds'] = $totalsec % 60;
  return $ret;
}


php at sarge dot ch
28-Jan-2004 01:58

Additional thisone here (didn't test it yet but should work :D):

<?php
/**
 * Calculates the Difference between two timestamps
 *
 * @param integer $start_timestamp
 * @param integer $end_timestamp
 * @param integer $unit (default 0)
 * @return string
 * @access public
 */
function dateDifference($start_timestamp,$end_timestamp,$unit= 0){
 
$days_seconds_star= (23 * 56 * 60) + 4.091; // Star Day
 
$days_seconds_sun= 24 * 60 * 60; // Sun Day
 
$difference_seconds= $end_timestamp - $start_timestamp;
  switch(
$unit){
   case
3: // Days
    
$difference_days= round(($difference_seconds / $days_seconds_sun),2);
     return
'approx. '.$difference_hours.' Days';
   case
2: // Hours
    
$difference_hours= round(($difference_seconds / 3600),2);
     return
'approx. '.$difference_hours.' Hours';
   break;
   case
1: // Minutes
    
$difference_minutes= round(($difference_seconds / 60),2);
     return
'approx. '.$difference_minutes.' Minutes';
   break;
   default:
// Seconds
    
if($difference_seconds > 1){
       return
$difference_seconds.' Seconds';
     }
     else{
       return
$difference_seconds.' Second';
     }
  }
}
?>


wwb_99 at yahoo dot com
25-Jan-2004 10:12

Handy little function getting the total difference in dates.

function DateDiff($tfirst, $tsecond)
{
   //returns an array with numeric values for in an array measuring days, hours, minutes & seconds
   $ret=array();
   $totalsec=$tsecond-$tfirst;
   $ret['days']=round(($totalsec/86400));
   $totalsec=$totalsec % 86400;
   $ret['hours']=round(($totalsec/3600));
   $totalsec=$totalsec % 3600;
   $ret['minutes']=round(($totalsec/60));
   $ret['seconds']=$totalsec % 60;
   return $ret;
}


php at elmegil dot net
20-Dec-2003 07:40

A much easier way to do days diff is to use Julian Days from the Calendar functions:

$start = gregoriantojd($smon, $sday, $syear);
$end = gregoriantojd($emon, $eday, $eyear);
$daysdiff = $end - $start;

You can see the obvious ways to wrap a function around that.


vincentv at thevoid dot demon dot nl
19-Nov-2003 10:56

A rectification to the some of the functions i posted a while ago.

They do not work correctly under all circumstances (in my small test cases they worked) which is due to the fact that when you create a date using mktime, which returns a certain amount of seconds, this is not valid for every month since each month has a different amount of seconds.

The solution is to break up the original timestamp, add to it's seperate parts and create a new timestamp.

Old:
=====
function sub($timestamp, $seconds,$minutes,$hours,$days,$months,$years) {
   $mytime = mktime(1+$hours,0+$minutes,0+$seconds,1+$months,1+$days,1970+$years);
   return $timestamp - $mytime;
}
function add($timestamp, $seconds,$minutes,$hours,$days,$months,$years) {
   $mytime = mktime(1+$hours,0+$minutes,0+$seconds,1+$months,1+$days,1970+$years);
   return $timestamp + $mytime;
}
=====

New:
=====
function add($timestamp, $seconds, $minutes, $hours, $days, $months, $years) {
   $timePieces = getdate($timestamp);
   return mktime(    $timePieces["hours"] + $hours,
                   $timePieces["minutes"] + $minutes,
                   $timePieces["seconds"] + $seconds,
                   $timePieces["mon"] + $months,
                   $timePieces["mday"] + $days,
                   $timePieces["year"] + $years );
}
function sub($timestamp, $seconds, $minutes, $hours, $days, $months, $years) {
   $timePieces = getdate($timestamp);
   return mktime(    $timePieces["hours"] - $hours,
                   $timePieces["minutes"] - $minutes,
                   $timePieces["seconds"] - $seconds,
                   $timePieces["mon"] - $months,
                   $timePieces["mday"] - $days,
                   $timePieces["year"] - $years );
}
=====

Regards,
- Vincent


CodeDuck at gmx dot net
07-Nov-2003 03:30

in reply to dkan at netscreen dot com 29-Aug-2003 07:40
> Zero-padding is easier to read and less complicated if you
> use the substr() function instead of an if-then statement.

my two versions of printtime with padding:

<?
function printtime() {
  
$timenow = getdate();
  
printf(
    
'%02d %02d %02d',
    
$timenow["hours"],
    
$timenow["minutes"],
    
$timenow["seconds"]
   );
}
?>

or the better one:
<?
function printtime() {
   echo
date('H i s');
}
?>


Xiven
02-Oct-2003 03:09

One thing PHP really lacks IMHO is an equivalent of ASP's "DateDiff" function.  Here's a function that comes close to duplicating the functionality:

<?php
function datediff($interval, $date1, $date2) {
  
// Function roughly equivalent to the ASP "DateDiff" function
  
$seconds = $date2 - $date1;
  
   switch(
$interval) {
       case
"y":
           list(
$year1, $month1, $day1) = split('-', date('Y-m-d', $date1));
           list(
$year2, $month2, $day2) = split('-', date('Y-m-d', $date2));
          
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
          
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
          
$diff = $year2 - $year1;
           if(
$month1 > $month2) {
              
$diff -= 1;
           } elseif(
$month1 == $month2) {
               if(
$day1 > $day2) {
                  
$diff -= 1;
               } elseif(
$day1 == $day2) {
                   if(
$time1 > $time2) {
                      
$diff -= 1;
                   }
               }
           }
           break;
       case
"m":
           list(
$year1, $month1, $day1) = split('-', date('Y-m-d', $date1));
           list(
$year2, $month2, $day2) = split('-', date('Y-m-d', $date2));
          
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
          
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
          
$diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1);
           if(
$day1 > $day2) {
              
$diff -= 1;
           } elseif(
$day1 == $day2) {
               if(
$time1 > $time2) {
                  
$diff -= 1;
               }
           }
           break;
       case
"w":
          
// Only simple seconds calculation needed from here on
          
$diff = floor($seconds / 604800);
           break;
       case
"d":
          
$diff = floor($seconds / 86400);
           break;
       case
"h":
          
$diff = floor($seconds / 3600);
           break;       
       case
"i":
          
$diff = floor($seconds / 60);
           break;       
       case
"s":
          
$diff = $seconds;
           break;       
   }   
   return
$diff;
}
?>


dkan at netscreen dot com
29-Aug-2003 10:40

Zero-padding is easier to read and less complicated if you use the substr() function instead of an if-then statement.

function printtime() {
   $timenow = getdate();
   $hours = substr("0" . $timenow["hours"], -2);
   $minutes = substr("0" . $timenow["minutes"], -2);
   $seconds = substr("0" . $timenow["seconds"], -2);

   print($hours . " " . $minutes . " " . $seconds);
}


bitbuster at example dot com
24-Jul-2003 05:01

If you have to compare timestamps, I suggest you do it inside the database.. postgres, for example, allows statements like this:

select (current_timestamp < (select zeitdatum from time_test where zahl=5) );


menaurus at gmx dot de
16-Jul-2003 01:37

The argument has to be in the standard mysql format (y-m-d)...

function age($date) {

if (!$date) return false;
$year=0+substr($date,0,4);
$month=0+substr($date,5,2);
$day=0+substr($date,8,2);
$t=0;
$d=date("d");
$m=date("m");
$y=date("Y");
$age=$y-$year;

if ($m<$month) $t=-1;
else if ($m==$month) if ($d<$day) $t=-1;

return ($age+$t);
}

this funktion has got a little bug:
On Line 12 and 13...
Bugfix:
12 if ($month<$m) $t=-1;
13 else if ($m==$month AND $day<$d) $t=-1;


you NOSPAM don't need 2 know ETC
24-Mar-2003 04:17

EXCEL DATES TO UNIX TIMESTAMPS
----------------------------

I get a lot of dates which are sent to me in those dastardly Excel spreadsheet things. For example, the date 15 April 1976, Excel stores as 27865.

I convert these to UNIX timestamps using the little function below.

<?
function xl2timestamp($xl_date)
{
$timestamp = ($xl - 25569) * 86400;
return
$timestamp;
}
?>


garyc at earthling dot net
19-Mar-2003 05:08

I needed to calculate the week number from a given date and vice versa, where the week starts with a Monday and the first week of a year may begin the year before, if the year begins in the middle of the week (Tue-Sun). This is the way weekly magazines calculate their issue numbers.

Here are two functions that do exactly that:

Hope somebody finds this useful.

Gary

/*  w e e k n u m b e r  -------------------------------------- //
weeknumber returns a week number from a given date (>1970, <2030)
Wed, 2003-01-01 is in week 1
Mon, 2003-01-06 is in week 2
Wed, 2003-12-31 is in week 53, next years first week
Be careful, there are years with 53 weeks.
// ------------------------------------------------------------ */

function weeknumber ($y, $m, $d) {
   $wn = strftime("%W",mktime(0,0,0,$m,$d,$y));
   $wn += 0; # wn might be a string value
   $firstdayofyear = getdate(mktime(0,0,0,1,1,$y));
   if ($firstdayofyear["wday"] != 1)    # if 1/1 is not a Monday, add 1
       $wn += 1;
   return ($wn);
}    # function weeknumber

/*  d a t e f r o m w e e k  ---------------------------------- //
From a weeknumber, calculates the corresponding date
Input: Year, weeknumber and day offset
Output: Exact date in an associative (named) array
2003, 12, 0: 2003-03-17 (a Monday)
1995,  53, 2: 1995-12-xx
...
// ------------------------------------------------------------ */

function datefromweek ($y, $w, $o) {

   $days = ($w - 1) * 7 + $o;

   $firstdayofyear = getdate(mktime(0,0,0,1,1,$y));
   if ($firstdayofyear["wday"] == 0) $firstdayofyear["wday"] += 7;
# in getdate, Sunday is 0 instead of 7
   $firstmonday = getdate(mktime(0,0,0,1,1-$firstdayofyear["wday"]+1,$y));
   $calcdate = getdate(mktime(0,0,0,$firstmonday["mon"], $firstmonday["mday"]+$days,$firstmonday["year"]));

   $date["year"] = $calcdate["year"];
   $date["month"] = $calcdate["mon"];
   $date["day"] = $calcdate["mday"];

   return ($date);

}    # function datefromweek


balin
16-Feb-2003 10:23

this function count days between $start and $end dates in mysql format (yyyy-mm-dd)
if one of paramters is 0000-00-00 will return 0
$start date must be less then $end
<?
//For Count Days
function count_days($start, $end)
{
   if(
$start != '0000-00-00' and $end != '0000-00-00' )
   {
      
$timestamp_start = strtotime($start);
      
$timestamp_end = strtotime($end);
       if(
$timestamp_start >= $timestamp_end ) return 0;
      
$start_year = date("Y",$timestamp_start);
      
$end_year = date("Y", $timestamp_end);
      
$num_days_start = date("z",strtotime($start));
      
$num_days_end = date("z", strtotime($end));
      
$num_days = 0;
      
$i = 0;
       if(
$end_year > $start_year )
       {
           while(
$i < ( $end_year - $start_year ) )
           {
            
$num_days = $num_days + date("z", strtotime(($start_year + $i)."-12-31"));
            
$i++;
           }
         }
         return (
$num_days_end + $num_days ) - $num_days_start;
   }
   else
   {
         return
0;
     }
}
?>


jlim at natsoft dot com dot my
26-Jan-2003 10:28

For a date() function that supports dates outside the range 1901 and 2038 (by using floats when needed) see adodb_date( ) in

http://php.weblogs.com/adodb_date_time_library


zan at stargeek dot com
24-Jan-2003 06:49

an update to my earlier webcalendar funciton this is a class that uses a table full of start and end dates to generate a webcalendar http://www.stargeek.com/scripts.php?script=11&cat=blog


brighn (a) yahoo (.) com
03-Jan-2003 06:46

I needed a function that determined the last Sunday of the month. Since it's made for the website's "next meeting" announcement, it goes based on the system clock; also, if today is between Sunday and the end of the month, it figures out the last Sunday of *next* month. lastsunday() takes no arguments and returns the date as a string in the form "January 26, 2003". I could probably have streamlined this quite a bit, but at least it's transparent code. =)

  /* The two functions calculate when the next meeting will
     be, based on the assumption that the meeting will be on
     the last Sunday of the month. */ 

  function getlast($mon, $year) {
   $daysinmonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   $days = $daysinmonth[$mon-1];
   if ($mon == 2 && ($year % 4) == 0 && (($year % 100) != 0 ||
   ($year % 400) == 0)) $days++;
   if ($mon == 2 && ($year % 4) == 0 && ($year % 1000) != 0) $days++;
   $lastday = getdate(mktime(0,0,0,$mon,$days,$year));
   $wday = $lastday['wday'];
   return getdate(mktime(0,0,0,$mon,$days-$wday,$year));
  }

  function lastsunday() {
   $today = getdate();
   $mon = $today['mon'];
   $year = $today['year'];
   $mday = $today['mday'];
   $lastsun = getlast($mon, $year);
   $sunday = $lastsun['mday'];
   if ($sunday < $mday) {
     $mon++;
     if ($mon = 13) {
       $mon = 1;
       $year++;
     }
     $lastsun = getlast($mon, $year);
     $sunday = $lastsun['mday'];
   }
   $nextmeeting = getdate(mktime(0,0,0,$mon,$sunday,$year));
   $month = $nextmeeting['month'];
   $mday = $nextmeeting['mday'];
   $year = $nextmeeting['year'];
   return "$month $mday, $year";
  }


visualmind at php dot net
25-Dec-2002 09:49

Here's a new function for Hejri (Hijri) date conversion, It has a better flawless calculation than the previous posted function and it's implemented to be an official alternative for the php DATE function which returns Arabic Translated date and optionally Hejri converted.
Note: to view arabic titles correctly change view-encoding to Arabic-Windows (windows-1256)

function arabicDate($format, $timestamp) {
   /*
       written by Salah Faya (visualmind@php.net) http://www.php4arab.info/scripts/arabicDate
         $format:
               [*]hj|ar|en:[jdl][Fmn][Yy][Aa]  (php.date function handles the rest chars)
               * will add <span dir=rtl lang=ar-sa>..</span>
           examples:
               echo arabicDate('hj:l d-F-Y

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt