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
<?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.";
?>
Checking credit card expiration
<?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.";
}
?>
Creating a Timestamp with mktime()
<html>
<head>
<title>Creating a Timestamp with mktime()</title>
</head>
<body>
<div>
<?php
$ts = mktime( 2, 30, 0, 5, 1, 2004 );
print date("m/d/y G.i:s", $ts);
print "<br/>";
print "The date is ";
print date("jS of F Y, \a\\t g.i a", $ts );
?>
</div>
</body>
</html>
Get the labor day
<?php
print "<table>";
print "<tr><th>Year</th><th>Labor Day</th></tr>";
for ($year = 2004; $year <= 2020; $year++) {
$stamp = mktime(12,0,0,9,1,$year);
$stamp = strtotime("monday", $stamp);
print "<tr><td>$year</td><td>";
print date("F j", $stamp);
print "</td></tr>\n";
}
print "</table>";
?>
int mktime ( [int hour [, int minute [, int second [, int month[, int day [, int year [, int is_dst]]]]]]] )
<?
$unixtime = mktime(22, 30, 0, 6, 20, 2005, -1);
?>
Making an epoch timestamp
<?
$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.";
?>
mktime.php
<?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!";
?>
mktime() produces a GMT timestamp.
<?php
echo "Output of mktime(): " . mktime() . ".<br>\n";
echo "Output of gmmktime(): " . gmmktime() . ".<br>\n";
echo "date/mktime: " . date("r", mktime()) . ".<br>\n";
echo "date/gmmktime: " . date("r", gmmktime()) . ".<br>\n";
echo "gmdate/mktime: " . gmdate("r", mktime()) . ".<br>\n";
echo "gmdate/gmmktime" . gmdate("r", gmmktime()) . ".<br>\n";
?>
Use mktime to create a time
<?
$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);
?>
Using mktime() and date()
<?php
$stamp = mktime(0,0,0,1,1,1986);
print date("l",$stamp);
?>