PHP/Date/mktime
Содержание
- 1 Calculating the difference between two dates
- 2 Checking credit card expiration
- 3 Creating a Timestamp with mktime()
- 4 Get the labor day
- 5 int mktime ( [int hour [, int minute [, int second [, int month[, int day [, int year [, int is_dst]]]]]]] )
- 6 Making an epoch timestamp
- 7 mktime.php
- 8 mktime() produces a GMT timestamp.
- 9 Use mktime to create a time
- 10 Using mktime() and date()
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 "Year | Labor 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>