PHP/Date/mktime

Материал из Web эксперт
Перейти к: навигация, поиск

Calculating the difference between two dates

   <source lang="html4strict">

<?php $epoch_1 = mktime(19,32,56,5,10,1965); $epoch_2 = mktime(4,29,11,11,20,1962); $diff_seconds = $epoch_1 - $epoch_2; $diff_weeks = floor($diff_seconds/604800); $diff_seconds -= $diff_weeks * 604800; $diff_days = floor($diff_seconds/86400); $diff_seconds -= $diff_days * 86400; $diff_hours = floor($diff_seconds/3600); $diff_seconds -= $diff_hours * 3600; $diff_minutes = floor($diff_seconds/60); $diff_seconds -= $diff_minutes * 60; print "The two dates have $diff_weeks weeks, $diff_days days, "; print "$diff_hours hours, $diff_minutes minutes, and $diff_seconds "; print "seconds elapsed between them."; ?>

 </source>
   
  


Checking credit card expiration

   <source lang="html4strict">

<?php $expires = mktime(0, 0, 0, $_POST["month"], 1, $_POST["year"]); $nextMonth = mktime(0, 0, 0, date("n") + 1, 1); if ($expires < $nextMonth) {

  print "Sorry, that credit card expires too soon.";

} ?>

 </source>
   
  


Creating a Timestamp with mktime()

   <source lang="html4strict">

<html> <head> <title>Creating a Timestamp with mktime()</title> </head> <body>

<?php $ts = mktime( 2, 30, 0, 5, 1, 2004 ); print date("m/d/y G.i:s", $ts); print "
"; print "The date is "; print date("jS of F Y, \a\\t g.i a", $ts ); ?>

</body> </html>

 </source>
   
  


Get the labor day

   <source lang="html4strict">

<?php

print ""; print "";

for ($year = 2004; $year <= 2020; $year++) {

   $stamp = mktime(12,0,0,9,1,$year);
   $stamp = strtotime("monday", $stamp);
print "\n"; } print "
YearLabor Day
$year";
   print date("F j", $stamp);
print "
";

?>

 </source>
   
  


int mktime ( [int hour [, int minute [, int second [, int month[, int day [, int year [, int is_dst]]]]]]] )

   <source lang="html4strict">

<?

   $unixtime = mktime(22, 30, 0, 6, 20, 2005, -1);

?>

 </source>
   
  


Making an epoch timestamp

   <source lang="html4strict">

<? $afternoon = mktime(13,30,45,10,20,2009); print strftime("At %I:%M:%S on %m/%d/%y, ", $afternoon); print "$afternoon seconds have elapsed since 1/1/1970."; ?>

 </source>
   
  


mktime.php

   <source lang="html4strict">

<?php $now = mktime(); $taxday = mktime(0,0,0,4,15,2006); $difference = $taxday - $now; $hours = round($difference / 60 / 60); echo "Only $hours hours until tax day!"; ?>

 </source>
   
  


mktime() produces a GMT timestamp.

   <source lang="html4strict">

<?php echo "Output of mktime(): " . mktime() . ".
\n"; echo "Output of gmmktime(): " . gmmktime() . ".
\n"; echo "date/mktime: " . date("r", mktime()) . ".
\n"; echo "date/gmmktime: " . date("r", gmmktime()) . ".
\n"; echo "gmdate/mktime: " . gmdate("r", mktime()) . ".
\n"; echo "gmdate/gmmktime" . gmdate("r", gmmktime()) . ".
\n"; ?>

 </source>
   
  


Use mktime to create a time

   <source lang="html4strict">

<? $stamp = mktime(19,45,0,10,20,2004); print strftime("Today is day %d of %B and day %j of the year %Y. The time is %I:%M %p (also known as %H:%M).", $stamp); ?>

 </source>
   
  


Using mktime() and date()

   <source lang="html4strict">

<?php $stamp = mktime(0,0,0,1,1,1986); print date("l",$stamp); ?>

 </source>