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()
<source lang="html4strict">
<?php $birthday = "March 10, 2010"; $whoopee_made = strtotime("$birthday - 9 months ago"); ?>
</source>
Checking a date range
<source lang="html4strict">
<? // 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.";
} ?>
</source>
Finding the Date for a Weekday
<source lang="html4strict">
<?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 "
The first Tuesday of " . date("F", $firsttue_ts) . " is " . date("d M Y", $firsttue_ts) . "."; }
?>
</source>
Find the First monday on or after November 1, 2008
<source lang="html4strict">
<? $monday = strtotime("Monday", $november); ?>
</source>
Find weekday
<source lang="html4strict">
<?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")) . "
"; echo date("d M Y", find_weekday(5, 2000, "Friday", 1)) . "
"; echo date("d M Y", find_weekday(5, 2000, "Fri", 2)) . "
"; echo date("d M Y", find_weekday(5, 2000, "Friday", 3)) . "
"; echo date("d M Y", find_weekday(5, 2000, "Friday", 4));
?>
</source>
Getting the Day and Week of the Year
<source lang="html4strict">
<?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) . "
\n";
} ?>
</source>
If PHP is unable to convert your string into a timestamp, it will return -1.
<source lang="html4strict">
<?
$mydate = strtotime("Christmas 1979"); if ($mydate == -1) { print "Date conversion failed!"; } else { print "Date conversion succeeded!"; }
?>
</source>
int strtotime ( string time [, int now] ) converts strings to a timestamp
<source lang="html4strict">
<?
print strtotime("22nd December 1979"); print strtotime("22 Dec. 1979 17:30"); print strtotime("1979/12/22");
?>
</source>
Obtaining the Difference Between Two Dates
<source lang="html4strict">
<?php
$date1 = "14 Jun 2002"; $date2 = "05 Feb 2006"; $ts1 = strtotime($date1); $ts2 = strtotime($date2);printf("
The difference between %s and %s is %d seconds.<p>\n", $date1, $date2, $ts2 - $ts1); ?> </source>
Subtracts a year from a given timestamp
<source lang="html4strict">
<?
print strtotime("1 year ago", 123456789);
?>
</source>
Using strtotime()
<source lang="html4strict">
<? $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); ?>
</source>
Using strtotime() with a starting epoch timestamp
<source lang="html4strict">
<? $november = mktime(0,0,0,11,1,2008); ?>
</source>