Php strtotime this monday

strtotime

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in baseTimestamp , or the current time if baseTimestamp is not supplied.

The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable .

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

Parameters

A date/time string. Valid formats are explained in Date and Time Formats.

The timestamp which is used as a base for the calculation of relative dates.

Return Values

Returns a timestamp on success, false otherwise.

Errors/Exceptions

Every call to a date/time function will generate a E_WARNING if the time zone is not valid. See also date_default_timezone_set()

Changelog

Version Description
8.0.0 baseTimestamp is nullable now.

Examples

Example #1 A strtotime() example

echo strtotime ( «now» ), «\n» ;
echo strtotime ( «10 September 2000» ), «\n» ;
echo strtotime ( «+1 day» ), «\n» ;
echo strtotime ( «+1 week» ), «\n» ;
echo strtotime ( «+1 week 2 days 4 hours 2 seconds» ), «\n» ;
echo strtotime ( «next Thursday» ), «\n» ;
echo strtotime ( «last Monday» ), «\n» ;
?>

Example #2 Checking for failure

if (( $timestamp = strtotime ( $str )) === false ) echo «The string ( $str ) is bogus» ;
> else echo » $str == » . date ( ‘l dS \o\f F Y h:i:s A’ , $timestamp );
>
?>

Notes

Note:

If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).

Note:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash ( / ), then the American m/d/y is assumed; whereas if the separator is a dash ( — ) or a dot ( . ), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash ( — ), the date string is parsed as y-m-d .

To avoid potential ambiguity, it’s best to use ISO 8601 ( YYYY-MM-DD ) dates or DateTime::createFromFormat() when possible.

Note:

Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() .

See Also

  • Date and Time Formats
  • DateTime::createFromFormat() — Parses a time string according to a specified format
  • checkdate() — Validate a Gregorian date
  • strptime() — Parse a time/date generated with strftime

User Contributed Notes 42 notes

I’ve had a little trouble with this function in the past because (as some people have pointed out) you can’t really set a locale for strtotime. If you’re American, you see 11/12/10 and think «12 November, 2010». If you’re Australian (or European), you think it’s 11 December, 2010. If you’re a sysadmin who reads in ISO, it looks like 10th December 2011.

The best way to compensate for this is by modifying your joining characters. Forward slash (/) signifies American M/D/Y formatting, a dash (-) signifies European D-M-Y and a period (.) signifies ISO Y.M.D.

echo date ( «jS F, Y» , strtotime ( «11.12.10» ));
// outputs 10th December, 2011

echo date ( «jS F, Y» , strtotime ( «11/12/10» ));
// outputs 12th November, 2010

echo date ( «jS F, Y» , strtotime ( «11-12-10» ));
// outputs 11th December, 2010
?>

Hope this helps someone!

The «+1 month» issue with strtotime
===================================
As noted in several blogs, strtotime() solves the «+1 month» («next month») issue on days that do not exist in the subsequent month differently than other implementations like for example MySQL.

echo date ( «Y-m-d» , strtotime ( «2009-01-31 +1 month» ) ); // PHP: 2009-03-03
echo date ( «Y-m-d» , strtotime ( «2009-01-31 +2 month» ) ); // PHP: 2009-03-31
?>

SELECT DATE_ADD ( ‘2009-01-31’ , INTERVAL 1 MONTH ); // MySQL: 2009-02-28
?>

UK dates (eg. 27/05/1990) won’t work with strotime, even with timezone properly set.

/*
However, if you just replace «/» with «-» it will work fine.
$timestamp = strtotime ( str_replace ( ‘/’ , ‘-‘ , ’27/05/1990’ ));
?>
*/

[red., derick]: What you instead should do is:

$date = date_create_from_format ( ‘d/m/y’ , ’27/05/1990′ );
?>

That does not make it a timestamp, but a DateTime object, which is much more versatile instead.

WARNING when using «next month», «last month», «+1 month», «-1 month» or any combination of +/-X months. It will give non-intuitive results on Jan 30th and 31st.

$d = new DateTime ( ‘2010-01-31’ );
$d -> modify ( ‘next month’ );
echo $d -> format ( ‘F’ ), «\n» ;
?>

In the above, using «next month» on January 31 will output «March» even though you might want it to output «February». («+1 month» will give the same result. «last month», «-1 month» are similarly affected, but the results would be seen at beginning of March.)

The way to get what people would generally be looking for when they say «next month» even on Jan 30 and Jan 31 is to use «first day of next month»:

$d = new DateTime ( ‘2010-01-08’ );
$d -> modify ( ‘first day of next month’ );
echo $d -> format ( ‘F’ ), «\n» ;
?>

$d = new DateTime ( ‘2010-01-08’ );
$d -> modify ( ‘first day of +1 month’ );
echo $d -> format ( ‘F’ ), «\n» ;
?>

I tried using sams most popular example but got incorrect results.

Incorrect:
echo date ( «jS F, Y» , strtotime ( «11.12.10» ));
// outputs 10th December, 2011

echo date ( «jS F, Y» , strtotime ( «11/12/10» ));
// outputs 12th November, 2010

echo date ( «jS F, Y» , strtotime ( «11-12-10» ));
// outputs 11th December, 2010
?>

Then I read the notes which said:
if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. ***If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.***

Therefore, the above code does not work on 2 digit years — only 4 digit years

strtotime() also returns time by year and weeknumber. (I use PHP 5.2.8, PHP 4 does not support it.) Queries can be in two forms:
— «yyyyWww», where yyyy is 4-digit year, W is literal and ww is 2-digit weeknumber. Returns timestamp for first day of week (for me Monday)
— «yyyy-Www-d», where yyyy is 4-digit year, W is literal, ww is 2-digit weeknumber and dd is day of week (1 for Monday, 7 for Sunday)

// Get timestamp of 32nd week in 2009.
strtotime ( ‘2009W32’ ); // returns timestamp for Mon, 03 Aug 2009 00:00:00
// Weeknumbers < 10 must be padded with zero:
strtotime ( ‘2009W01’ ); // returns timestamp for Mon, 29 Dec 2008 00:00:00
// strtotime(‘2009W1’); // error! returns false

// See timestamp for Tuesday in 5th week of 2008
strtotime ( ‘2008-W05-2’ ); // returns timestamp for Tue, 29 Jan 2008 00:00:00
?>

Weeknumbers are (probably) computed according to ISO-8601 specification, so doing date(‘W’) on given timestamps should return passed weeknumber.

The difference between ‘today’ and ‘now’ is the former strips off the time (setting it to 00:00, ie. midnight just past), and the latter includes the time — assuming UTC unless specified otherwise.

I run a theatre’s website. Obviously, I need to ensure shows that have already happened do not appear on web pages, so I use something on the lines of:

$listIt = (strtotime($end_date) >= strtotime(‘today’) ? 1 : 0);

where $end_date is the final date in a show series. So if tonight’s show is the last, it will stay on the web page until 00:00 tomorrow.

I don’t normally include performance times in the date field because some shows have matinées, others don’t — so I use a free-form performance time field in the CMS instead (where even ‘Time: TBD’ is allowed). [The only instance where I DO include the time is when there are two different shows on the same day, to ensure they appear in chronological order.]

So strtotime($end_date) will always return the timestamp at 00:00 that day. If I instead used:

$listIt = (strtotime($end_date) >= strtotime(‘now’) ? 1 : 0);

the function would return $listIt = 0 at all times today — something I don’t want.

One important thing you should remember is that the timestamp value returned by time() is time-zone agnostic and gets the number of seconds since 1 January 1970 at 00:00:00 UTC. This means that at a particular point in time, this function will return the same value in the US, Europe, India, Japan, .

strtotime() will convert a string WITHOUT a timezone indication as if the string is a time in the default timezone ( date_default_timezone_set() ). So converting a UTC time like ‘2018-12-06T09:04:55’ with strtotime() actually yields a wrong result. In this case use:

function UTCdatestringToTime ( $utcdatestring )
$tz = date_default_timezone_get ();
date_default_timezone_set ( ‘UTC’ );

$result = strtotime ( $utcdatestring );

date_default_timezone_set ( $tz );
return $result ;
>
?>

Test:
$tz = ‘Europe/Amsterdam’ ;
$utctime = ‘2018-12-06T09:04:55’ ;

if (! date_default_timezone_set ( $tz )) WriteLine ( ‘Setting default timezone to ‘ . $tz . ‘ failed’ );
die;
>

WriteLine ();
WriteLine ( ‘Default timezone set to ‘ . $tz );
WriteLine ( ‘UTC time: ‘ . $utctime );
WriteLine ();

WriteLine ( ‘[ UTCdatestringToTime ]’ );
$phptime = UTCdatestringToTime ( $utctime );
WriteLine ( ‘PHP time: ‘ . $phptime );
WriteTime ( $phptime , true );
WriteTime ( $phptime , false );
WriteLine ();

WriteLine ( ‘——————————————————————————-‘ );
WriteLine ( ‘[ strtotime($utctime) — Converts $utctime as if it was in timezone ‘ . $tz . ‘ because the string has no timezone specification ]’ );
$phptime = strtotime ( $utctime ); //Seconds since the unix epoch
WriteLine ( ‘PHP time: ‘ . $phptime );
WriteTime ( $phptime , true );
WriteTime ( $phptime , false );

function WriteLine ( $text = » )
echo $text . «\r\n» ;
>

function WriteTime ( $time , bool $asUTC )
$tz = date_default_timezone_get ();
if ( $asUTC ) date_default_timezone_set ( ‘UTC’ );

WriteLine ( ‘—> (‘ . date_default_timezone_get () . ‘) ‘ . date ( ‘Y-m-d H:i:s’ , $time ));

if ( $asUTC ) date_default_timezone_set ( $tz );
>
?>

Test output:

Default timezone set to Europe / Amsterdam
UTC time : 2018 — 12 — 06T09 : 04 : 55

[ UTCdatestringToTime ]
PHP time : 1544087095
—> ( UTC ) 2018 — 12 — 06 09 : 04 : 55
—> ( Europe / Amsterdam ) 2018 — 12 — 06 10 : 04 : 55

——————————————————————————-
[ strtotime ( $utctime ) — Converts $utctime as if it was in timezone Europe / Amsterdam because the string has no timezone specification ]
PHP time : 1544083495
—> ( UTC ) 2018 — 12 — 06 08 : 04 : 55
—> ( Europe / Amsterdam ) 2018 — 12 — 06 09 : 04 : 55
*/
?>

For negative UNIX timestamps, strtotime seems to return the literal you passed in, or it may try to deduct the number of seconds from today’s date.

To work around this behaviour, it appears that the same behaviour as described in the DateTime classes applies:

Specifically this line here (in the EN manual):

> The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

Therefore strtotime(‘@-1000’) returns 1000 seconds before the epoch.

You are not restricted to the same date ranges when running PHP on a 64-bit machine. This is because you are using 64-bit integers instead of 32-bit integers (at least if your OS is smart enough to use 64-bit integers in a 64-bit OS)

The following code will produce difference output in 32 and 64 bit environments.

32-bit PHP: bool(false)
64-bit PHP: int(-30607689600)

This is true for php 5.2.* and 5.3

Also, note that the anything about the year 10000 is not supported. It appears to use only the last digit in the year field. As such, the year 10000 is interpretted as the year 2000; 10086 as 2006, 13867 as 2007, etc

exemplo strtotime(date(«Y-m-d») . «+1month»);

A strtotime também funciona quando concatenamos strings,

Источник

Читайте также:  Javascript var string functions
Оцените статью