PHP/String/strtotime
Содержание
- 1 Calculating a date interval with strtotime()
- 2 Checking a date range
- 3 Finding the Date for a Weekday
- 4 Find the First monday on or after November 1, 2008
- 5 Find weekday
- 6 Getting the Day and Week of the Year
- 7 If PHP is unable to convert your string into a timestamp, it will return -1.
- 8 int strtotime ( string time [, int now] ) converts strings to a timestamp
- 9 Obtaining the Difference Between Two Dates
- 10 Subtracts a year from a given timestamp
- 11 Using strtotime()
- 12 Using strtotime() with a starting epoch timestamp
Calculating a date interval with strtotime()
<?php
$birthday = "March 10, 2010";
$whoopee_made = strtotime("$birthday - 9 months ago");
?>
Checking a date range
<?
// Get the epoch timestamp for 6 months ago
$range_start = strtotime("6 months ago");
// Get the epoch timestamp for right now
$range_end = time();
$submitted_date = strtotime($_POST["yr"] . "-" .
$_POST["mo"] . "-" .
$_POST["dy"]);
if (($range_start > $submitted_date) || ($range_end < $submitted_date)) {
$errors[] = "Please choose a date less than six months old.";
}
?>
Finding the Date for a Weekday
<?php
echo "Today is " . date("d M Y") . ".";
for($i = 1; $i <= 12; $i++)
{
$nextmonth = date("Y-" . (date("n") + $i) . "-01");
$nextmonth_ts = strtotime($nextmonth);
$firsttue_ts = strtotime("Tuesday", $nextmonth_ts);
echo "<br />The first Tuesday of " . date("F", $firsttue_ts)
. " is " . date("d M Y", $firsttue_ts) . ".";
}
?>
Find the First monday on or after November 1, 2008
<?
$monday = strtotime("Monday", $november);
?>
Find weekday
<?php
function find_weekday($month, $year, $weekday, $offset=1)
{
$month_ts = strtotime("$year-$month-01");
if(--$offset > 0)
$month_ts = strtotime("+$offset week", $month_ts);
$month_ts = strtotime($weekday, $month_ts);
return $month_ts;
}
echo date("d M Y", find_weekday(5, 2000, "Friday")) . "<br />";
echo date("d M Y", find_weekday(5, 2000, "Friday", 1)) . "<br />";
echo date("d M Y", find_weekday(5, 2000, "Fri", 2)) . "<br />";
echo date("d M Y", find_weekday(5, 2000, "Friday", 3)) . "<br />";
echo date("d M Y", find_weekday(5, 2000, "Friday", 4));
?>
Getting the Day and Week of the Year
<?php
$mydates = array("2010-01-01", "2010-06-30", "2010-12-31");
foreach($mydates as $mydate) {
$ts = strtotime($mydate);
echo "Day " . date("d M Y: z", $ts) . "<br />\n";
}
?>
If PHP is unable to convert your string into a timestamp, it will return -1.
<?
$mydate = strtotime("Christmas 1979");
if ($mydate == -1) {
print "Date conversion failed!";
} else {
print "Date conversion succeeded!";
}
?>
int strtotime ( string time [, int now] ) converts strings to a timestamp
<?
print strtotime("22nd December 1979");
print strtotime("22 Dec. 1979 17:30");
print strtotime("1979/12/22");
?>
Obtaining the Difference Between Two Dates
<?php
$date1 = "14 Jun 2002";
$date2 = "05 Feb 2006";
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
printf("<p>The difference between %s and %s is %d seconds.<p>\n",
$date1, $date2, $ts2 - $ts1);
?>
Subtracts a year from a given timestamp
<?
print strtotime("1 year ago", 123456789);
?>
Using strtotime()
<?
$now = time();
$later = strtotime("Thursday",$now);
$before = strtotime("last thursday",$now);
print strftime("now: %c \n", $now);
print strftime("later: %c \n", $later);
print strftime("before: %c \n", $before);
?>
Using strtotime() with a starting epoch timestamp
<?
$november = mktime(0,0,0,11,1,2008);
?>