Strtotime Accepted Formats For Essays - Homework for you

Homework for you

Strtotime Accepted Formats For Essays

Rating: 4.5/5.0 (18 Votes)

Category: Essay


PHP function to identify and parse unusual time format? Stack Overflow

I'm currently working with weather data feeds to assemble some accurate data. This particular feed is being pulled from Norway and has a format of 2012-01-13T19:00:00. The "T" is what throws me off.

Does anyone know if PHP will recognize this format and/or provide a function to parse this particular timestamp?

I would have thought a combo of strtotime() and date(), but apparently it only accepts English formats? I would prefer to avoid doing a regex to strip out the T but if it's necessary I'll work with it.

Many thanks for the help.

asked Jan 13 '12 at 5:37

131k ● 25 ● 202 ● 367

answered Jan 13 '12 at 5:39

Perfect! I didn't know it was a standard. Also nice to discover codepad, it's kinda like jsFiddle. I'll accept the answer ASAP (unless someone provides a rather detailed and awesome essay LOL) – crockpotveggies Jan 13 '12 at 5:42

answered Jan 13 '12 at 5:53

although deceze answered it quite well, this will be helpful for parsing any feeds that look unstandardized. thanks! – crockpotveggies Jan 13 '12 at 5:54

2016 Stack Exchange, Inc

Other articles

Comparing two Dates using strtotime() in PHP - Stack Overflow

Works here? date_default_timezone_set('Europe/London'); $getCurrentDate = str_replace('/', '-',$_REQUEST['date']); $four_days_have_passed = date('m-d-Y', strtotime($getCurrentDate. "+4 days")); $now = date('m-d-Y', strtotime($rep1. "+0 day")); echo $four_days_have_passed; echo "<br>".$now; if ($now < $four_days_have_passed) < echo "<br> booya"; > – David Folksman Jul 9 '13 at 23:24

I see you accepted answer but just to make sure you understand. There are 2 cases that dates are parsed.

  1. American - month/day/year
  2. European - day.month.year or day-month-year

You have error because you provided european format "07-14-2013" and there's no 14 month in year.

The proper format for you is one of these:

  • 14-07-2013 - Europe
  • 14.07.2013 - Europe
  • 07/14/2013 - American

Morover, to compare datatime it's better to use object oriented solution and DataTime object. You can read more here.

Strtotime() doesn - t work with dd

You can parse dates from a custom format (as of PHP 5.3) with DateTime::createFromFormat

(Aside: The. is used to reset non-specified values to the Unix timestamp, ie. the time will be midnight.)

If you do not want to (or cannot) use PHP 5.3, then a full list of available date/time formats which strtotime accepts is listed on the Date Formats manual page. That page more thoroughly describes the fact that m/d/Y is inferred over d/m/Y (but you can, as mentioned in the answers here, use d-m-Y. d.m.Y or d\tm\tY ).

In the past, I've also resorted to the quicky str_replace mentioned in another answer, as well as self-parsing the date string into another format like

answered May 23 '10 at 14:01

From the STRTOTIME writeup 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.

It is as simple as that.

answered Oct 10 '12 at 8:11

We can implement that as they won't give error but when I checked $t and $t1 are showing different result. I checked like this. $t=strtotime(date('d-m-Y')); and $t1=strtotime(date('m/d/Y')); – vusan Oct 10 '12 at 9:03

This is a good solution to many problems:

answered Jan 17 '11 at 1:13

You can add or remove formats depending on your needs. – Jonathan Roy Jan 17 '11 at 1:16

I found this useful, thank you. – Jon C. Aug 11 '15 at 1:41

fastest should probably be

this will return false if the format does not look like the proper one, but it wont-check wether the date is valid

I haven't found a better solution. You can use explode(). preg_match_all(). etc.

I have a static helper function like this

There is probably a better name for that, but I use ausStrToTime() because it works with Australian dates (which I often deal with, being an Australian). A better name would probably be the standardised name, but I'm not sure what that is.

answered May 23 '10 at 13:47

258k ● 122 ● 631 ● 788

Are you getting this value from a database? If so, consider formatting it in the database (use date_format in mysql, for example). If not, exploding the value may be the best bet, since strtotime just doesn't seem to appreciate dd/mm/yyyy values.

answered May 23 '10 at 13:47

no, it's value from jquery calendar – Syom May 23 '10 at 13:48

If you know it's in dd/mm/YYYY, you can do:

It will match dates in the form d/m/YY or dd/mm/YYYY (or any combination of the two).

If you want to support more separators than just /, you can change the regex to:

And then add any characters you want into the [/-] bit (Note, the - character needs to be last)

PHP strtotime() Function

PHP strtotime() Function Example

Parse English textual datetimes into Unix timestamps:

echo(strtotime("now"). "<br>");
echo(strtotime("3 October 2005"). "<br>");
echo(strtotime("+5 hours"). "<br>");
echo(strtotime("+1 week"). "<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds"). "<br>");
echo(strtotime("next Monday"). "<br>");
echo(strtotime("last Sunday"));

Definition and Usage

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.


PHP Function strtotime()

PHP - Function strtotime() Syntax Definition and Usage

The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp elative to the timestamp given in now, or the current time if none is supplied.

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.


Parameter & Description

Required. Specifies the time string to parse

Optional. The timestamp used to calculate the returned value.

Return Value

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.


Following is the usage of this function −

This will the produce following result −

How I Learned To Stop Using strtotime() And Love PHP DateTime

How I Learned To Stop Using strtotime() And Love PHP DateTime

Friday, March 1, 2013 - 15:50

The DateTime classes in PHP have been available since version 5.2, but I have largely ignored them until recently. This was partly due to the fact that I was working in PHP 5.1 environments a lot (don't ask) but mostly because I was just used to using the standard date functions that have always been a part of PHP (well, since version 4). I wanted to explain why I will be using the new DateTime classes more from now on and why you shouldn't be hesitant to use them.

Using a combination of strtotime() and date() can handle most things and is a good method to quickly grab a date.

You can get real problems, however, when the string isn't quite what strtotime() expects, which is especially true when trying to parse a date in a non-US date format. If the date string isn't understood by strtotime() then it will return false.

When this happens you can try a few things to force strtotime() to parse the date correctly. Sometimes it's something as simple as swapping the slashes for dashes, which forces strtotime() to parse the date in a different way.

However, this gets more complicated when the date string consists of more than just the date. Take the following date string, which I found being returned from a web service I was using.

Thu, 23/02/2012 - 15:18

Trying the previous trick of attempting to replace slashes doesn't work here, so further steps needed to be taken. After a couple of minutes of playing around with the string I found that removing the dash (-) between the date and the time caused the strtotime() function to accept the string and parse it correctly.

It was at this point that I questioned what I was trying to do. In my attempt to get the date string parsed I had used three expensive string manipulation functions, just to try and get the date time string into a state that strtotime() could understand. I therefore resolved to find a better solution to this problem and after a bit of research I re-discovered the PHP DateTime classes. Of particular interest was a method called DateTime::createFromFormat() that takes a format and a string and creates a date object. Here is an example of using the createFromFormat() method to parse the above date time value correctly.

My ultimate goal in all this was to simply compare one date with another as part of a usort() function call. What I initially started doing was converting the time into a timestamp and then compared those timestamps. With the PHP DateTime classes this isn't needed as the objects are directly comparable (at least since version 5.2.2). Here is an example of comparing two different DateTime objects.

Rather than attempt to hack the string into a working format and push it into strtotime() it is best to use the DateTime classes so that you are absolutely sure of what dates you get from your inputs. In fact, if you are unsure of the input format of your date then you need to take a step back and understand why this is the case. You have to be sure about what input parameters are being passed to any program, and if you are receiving a mix of different date formats then things can go horribly wrong very quickly.

The good thing about the createFromFormat() method is that if you do give it the wrong date format then it will return false. This means that you do have some leeway when creating DateTime objects and can correctly detect if things are not as they appear to be.

This is much more robust than using the DateTime class constructor which will throw an exception if you supply it with an invalid date format. Here is an example of producing an exception with the PHP DateTime class.

PHP Date - Time Functions - PHP Functions - PHP Tutorial - PHP For

Parses an English Textual Date or Time Into a Unix Timestamp

Since we will be using the word "timestamp" throughout this page, we will begin by explaining what it is. A Unix Timestamp is the number of seconds that have passed since January 1, 1970. Currently, the timestamp is 1469479851, but since it changes every second, the number will increase if you refresh the page. (The timestamp reflects the time set on the server. and not on the client's computer.)

The PHP Time() Function

The time() function returns the current timestamp. The timestamp that you see in the previous paragraph is generated by a very small line of code: <?php echo time(); ?>

The time() function can also return a modified timestamp. You can add or subtract any number of seconds to the function in order to return the timestamp of a previous or upcoming date and time.

For example, to return a timestamp for next week, or to return a timestamp from last week, I can add or subtract 7 days by determining how many seconds are involved (7 days * 24 hours per day * 60 minutes in an hour * 60 seconds in an hour = number of second in 7 days).

$last_week = time() - (7 * 24 * 60 * 60);
$next_week = time() + (7 * 24 * 60 * 60);
$next_month = time() + (30 * 24 * 60 * 60);
echo "Last Week: ". $last_week. "<br />";
echo "Next Week: ". $next_week. "<br />";
echo "Next Month: ". $next_month. "<br />";

The result will be:

Last Week: 1468875051
Next Week: 1470084651
Next Month: 1472071851

The PHP Strtotime() Function

The strtotime() function accepts an English datetime description and turns it into a timestamp. It is a simple way to determine "next week" or "last monday" without using the time() function and a bunch of math.

Some examples are:

echo strtotime("now"). "<br />";
echo strtotime("tomorrow"). "<br />";
echo strtotime("yesterday"). "<br />";
echo strtotime("10 September 2000"). "<br />";
echo strtotime("+1 day"). "<br />";
echo strtotime("+1 week"). "<br />";
echo strtotime("+1 week 2 days 4 hours 2 seconds"). "<br />";
echo strtotime("next Thursday"). "<br />";
echo strtotime("last Monday"). "<br />";
echo strtotime("4pm + 2 Hours"). "<br />";
echo strtotime("now + 2 fortnights"). "<br />";
echo strtotime("last Monday"). "<br />";
echo strtotime("2pm yesterday"). "<br />";
echo strtotime("7am 12 days ago"). "<br />";

The PHP Date() Function

The date() function formats a timestamp so that it actually makes sense, such as 1:50 PM Monday, July 25, 2016.

The date() function accepts two arguments, according to the following syntax: date(format, timestamp);. The first argument is the format that you want the timestamp in. The second argument (optional) is the timestamp that you want formatted. If no timestamp is supplied, the current timestamp will be used.

PHP provides over thirty-five case-sensitive characters that are used to format the date and time. These characters are:

Using a combination of these characters and commas, periods, dashes, semicolons and backslashes, you can now format dates and times.

// Will Echo: 1:50 PM Monday, July 25, 2016
echo date("g:i A l, F d, Y");

// Will Echo: 2016-07-24
$yesterday = strtotime("yesterday");
echo date("Y-m-d", $yesterday);

Calculating Past & Future Dates

It is very simple to compare two dates in php, to verify if one is past, present or future.

Since all dates can be converted back into timestamps, all it takes is a simple comparison of the two timestamps, which are essentially numbers. If the date in question is a bigger number than the current date, than you know that your date is in the future, and if the date is smaller than the current date, you know that the date is already past.

$my_date = strtotime("10 April 2005");
if(strtotime($my_date) > time()) <
echo "Future Date!";
> if(strtotime($my_date)

PHP Functions – Quickly test PHP calls within your browser

This is your credit balance. Even if you are an anonymous user, you are given some credits to spend. Every IP address has its own account and it is provided with free credits that can be used to pay for Online Domain Tools services. Moreover, credit balance is reset every day. This is why we call them Daily Credits. Registered users have higher Daily Credits amounts and can even increase them by purchasing subscriptions.

Besides Daily Credits, all accounts, including IP address accounts of anonymous users, have their credit Wallet. Wallet credits are not reset on a daily basis, but they are only spent when a user has not enough Daily Credits. Registered users can buy credits to their wallets. All IP address accounts are created with an initial Wallet balance of 3.00. Once IP address account spends credits from its Wallet, it can not be charged again. This should allow new users to try most of Online Domain Tools services without registration.

PHP Functions Online does not work with disabled Javascript. Please enable it and reload the page.

strtotime — Parse about any English textual datetime description into a Unix timestamp

Description #

int strtotime ( string $time [, int $now = time() ] )

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. Previous to PHP 5.1.0, this function would return -1 on failure.

Errors #

Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable. See also date_default_timezone_set()

Examples #

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" ;

Changelog #

Prior to PHP 5.3.0, relative time formats supplied to the time argument of strtotime() such as this week. previous week. last week. and next week were interpreted to mean a 7 day period relative to the current date/time, rather than a week period of Monday through Sunday.

Prior to PHP 5.3.0, 24:00 was not a valid format and strtotime() returned FALSE.

In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

Now returns FALSE on failure, instead of -1.

Notes #

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).

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.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

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.

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.

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

Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.