473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bad usage of strtotime?

Hi group,

I have been using strtotime a lot in my code.
I wonder if I made a mistake in my thinking. :-/
Here follows a stripped down example.

consider some dates:
$date1 = "2008-02-23";
$date2 = "2008-02-23";
(They are the same)

Suppose I need to know if date1 is smaller/equal or bigger than date2.
I often used code like:
$UTSdate1 = strtotime($date 1);
$UTSdate2 = strtotime($date 2);

if ($UTSdate1 <= $UTSdate2){
// date1 before or equal to date2
} else {
// date1 after date2
}

Good, not?

I think not after rereading the manual:

FROM: http://nl2.php.net/manual/en/function.strtotime.php
int strtotime ( string $time [, int $now ] )

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 (the
number of seconds since January 1 1970 00:00:00 GMT), relative to the
timestamp given in now , or the current time if now is not supplied.
The part "relative to the timestamp given in now" got me confused.

My question: Since the strtotime() is calculating 2 times the Unix Time
Stamp, do I risk that during execution of my script the second is
increased between the first call to strtotime() and the second, thus
making my above approach invalid?

Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);

Should I change my code according to the second approach (using $UTSnow)?
I know chances of ticking a second are small in such a small piece of
code, but I don't want to deliver code that 'works most of the time'.

Thanks for any insights.

Regards,
Erwin Moller
Jun 2 '08 #1
9 1825
Erwin Moller schreef:

<snip typo correction)
Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);
should read of course:
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$UTSnow);
$UTSdate2 = strtotime($date 2,$UTSnow);

Erwin
Jun 2 '08 #2
On Thu, 24 Apr 2008 10:49:41 +0200, Erwin Moller
<Si************ *************** *************** @spamyourself.c omwrote:
Erwin Moller schreef:

<snip typo correction)
>Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);

should read of course:
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$UTSnow);
$UTSdate2 = strtotime($date 2,$UTSnow);
Giving an explicit date will render the 'relative' part useless:
<?php
date_default_ti mezone_set('Eur ope/Amsterdam');
$date = "2008-02-23";
$UTSdate1 = strtotime($date );
sleep(2);
$UTSdate2 = strtotime($date );
var_dump($UTSda te1,$UTSdate2);
?>
Output:
int(1203721200)
int(1203721200)

However, giving a relative date, they can differ:
<?php
date_default_ti mezone_set('Eur ope/Amsterdam');
$date = "-2 hours";
//without supplying relative to what:
$UTSdate1 = strtotime($date );
sleep(2);
$UTSdate2 = strtotime($date );
var_dump($UTSda te1,$UTSdate2);
//with supplying a previously created timestamp:
$now = time();
$UTSdate1 = strtotime($date ,$now);
sleep(2);
$UTSdate2 = strtotime($date ,$now);
var_dump($UTSda te1,$UTSdate2);
?>
int(1209020255)
int(1209020257)
int(1209020257)
int(1209020257)

So you only have a problem with 'relative' datestrings ( +/- N hours,
'last Saturday', etc.). Specific datestrings are not relative to anything
in this context, so will yield the same output.
--
Rik Wasmus
Jun 2 '08 #3
On Thu, 24 Apr 2008 11:00:45 +0200, Rik Wasmus
<lu************ @hotmail.comwro te:
On Thu, 24 Apr 2008 10:49:41 +0200, Erwin Moller
<Si************ *************** *************** @spamyourself.c omwrote:
>Erwin Moller schreef:

<snip typo correction)
>>Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);

should read of course:
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$UTSnow);
$UTSdate2 = strtotime($date 2,$UTSnow);

Giving an explicit date will render the 'relative' part useless:
<?php
date_default_ti mezone_set('Eur ope/Amsterdam');
$date = "2008-02-23";
$UTSdate1 = strtotime($date );
sleep(2);
$UTSdate2 = strtotime($date );
var_dump($UTSda te1,$UTSdate2);
?>
Output:
int(1203721200)
int(1203721200)

However, giving a relative date, they can differ:
<?php
date_default_ti mezone_set('Eur ope/Amsterdam');
$date = "-2 hours";
//without supplying relative to what:
$UTSdate1 = strtotime($date );
sleep(2);
$UTSdate2 = strtotime($date );
var_dump($UTSda te1,$UTSdate2);
//with supplying a previously created timestamp:
$now = time();
$UTSdate1 = strtotime($date ,$now);
sleep(2);
$UTSdate2 = strtotime($date ,$now);
var_dump($UTSda te1,$UTSdate2);
?>
int(1209020255)
int(1209020257)
int(1209020257)
int(1209020257)

So you only have a problem with 'relative' datestrings ( +/- N hours,
'last Saturday', etc.). Specific datestrings are not relative to
anything in this context, so will yield the same output.
And on a sidenote: depending on the circumstances, I usually compare
timestamps which have to match within a margin of error (kind of like
float comparisons):

if($date1 - $error < $date2 < $date1 + $error){
//equal enough for this comparison
} else {
//outside margin of error
}
--
Rik Wasmus
Jun 2 '08 #4
Rik Wasmus schreef:
On Thu, 24 Apr 2008 11:00:45 +0200, Rik Wasmus
<lu************ @hotmail.comwro te:
>On Thu, 24 Apr 2008 10:49:41 +0200, Erwin Moller
<Si*********** *************** *************** *@spamyourself. comwrote:
>>Erwin Moller schreef:

<snip typo correction)

Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);
should read of course:
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$UTSnow);
$UTSdate2 = strtotime($date 2,$UTSnow);

Giving an explicit date will render the 'relative' part useless:
<?php
date_default_t imezone_set('Eu rope/Amsterdam');
$date = "2008-02-23";
$UTSdate1 = strtotime($date );
sleep(2);
$UTSdate2 = strtotime($date );
var_dump($UTSd ate1,$UTSdate2) ;
?>
Output:
int(1203721200 )
int(1203721200 )

However, giving a relative date, they can differ:
<?php
date_default_t imezone_set('Eu rope/Amsterdam');
$date = "-2 hours";
//without supplying relative to what:
$UTSdate1 = strtotime($date );
sleep(2);
$UTSdate2 = strtotime($date );
var_dump($UTSd ate1,$UTSdate2) ;
//with supplying a previously created timestamp:
$now = time();
$UTSdate1 = strtotime($date ,$now);
sleep(2);
$UTSdate2 = strtotime($date ,$now);
var_dump($UTSd ate1,$UTSdate2) ;
?>
int(1209020255 )
int(1209020257 )
int(1209020257 )
int(1209020257 )

So you only have a problem with 'relative' datestrings ( +/- N hours,
'last Saturday', etc.). Specific datestrings are not relative to
anything in this context, so will yield the same output.

And on a sidenote: depending on the circumstances, I usually compare
timestamps which have to match within a margin of error (kind of like
float comparisons):

if($date1 - $error < $date2 < $date1 + $error){
//equal enough for this comparison
} else {
//outside margin of error
}
Thanks a lot Rik.
Very clear explanation. :-)

My confusion was clearly based on my total misunderstandin g of the
'relative to' part, which was about a passed string like '+5 hours'.

Confusion cleared up now, and I feel a bit stupid too for asking. ;-)

Regards,
Erwin Moller
Jun 2 '08 #5
On 24 Apr, 09:47, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
I think not after rereading the manual:

FROM:http://nl2.php.net/manual/en/function.strtotime.php
int strtotime ( string $time [, int $now ] )

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 (the
number of seconds since January 1 1970 00:00:00 GMT), relative to the
timestamp given in now , or the current time if now is not supplied.
Hi Erwin,
in another recent thread I mentioned that the statement "The function
expects to be given a string containing a US English date format" is
contradicted later in the same page.
Jun 2 '08 #6
Captain Paralytic schreef:
On 24 Apr, 09:47, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
>I think not after rereading the manual:

FROM:http://nl2.php.net/manual/en/function.strtotime.php
> int strtotime ( string $time [, int $now ] )

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 (the
number of seconds since January 1 1970 00:00:00 GMT), relative to the
timestamp given in now , or the current time if now is not supplied.
Hi Erwin,
in another recent thread I mentioned that the statement "The function
expects to be given a string containing a US English date format" is
contradicted later in the same page.
Hi,

Yes, I can hardly say '+5 days' is an English date format.

Since we both get confused by this entry in the manual, I suggest
somebody rewrites it.
Or, of course, we both suck and should find another job than programming
PHP. ;-)
Anyway, Rik cleared up my misunderstandin g of that relative part, and I
think I keep programming PHP.

Regards,
Erwin Moller
Jun 2 '08 #7
On Thu, 24 Apr 2008 11:55:08 +0200, Erwin Moller
<Si************ *************** *************** @spamyourself.c omwrote:
Captain Paralytic schreef:
>On 24 Apr, 09:47, Erwin Moller
<Since_humans_ read_this_I_am_ spammed_too_m.. .@spamyourself. comwrote:
>>I think not after rereading the manual:

FROM:http://nl2.php.net/manual/en/function.strtotime.php
>> int strtotime ( string $time [, int $now ] )

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 (the
number of seconds since January 1 1970 00:00:00 GMT), relative to the
timestamp given in now , or the current time if now is not supplied.
Hi Erwin,
in another recent thread I mentioned that the statement "The function
expects to be given a string containing a US English date format" is
contradicted later in the same page.

Yes, I can hardly say '+5 days' is an English date format.
Well, '+5 dagen' or '+5 jours' is not understood, so I'd say it's english,
allthough not a date format :P
Since we both get confused by this entry in the manual, I suggest
somebody rewrites it.
Or, of course, we both suck and should find another job than programming
PHP. ;-)
Anyway, Rik cleared up my misunderstandin g of that relative part, and I
think I keep programming PHP.
The manual of strtotime can be confusing yes. When in doubt, I just test,
which leads me to believe i should just take it as 'strtotime will expect
a dd/dd[/dd[dd]] string to be in US format' (and for instance not
dd-dd-dddd):
echo strtotime('02-03-1980');//320799600, 1980-03-02
echo strtotime('02/03/1980');//318380400, 1980-02-03
echo strtotime('02-03');//unrecognized, 1970-01-01
echo strtotime('02/03');//US, 2008-02-03

Indeed not clear from the manual, just trial and error...
--
Rik Wasmus
Jun 2 '08 #8
Erwin Moller wrote:
Hi group,

I have been using strtotime a lot in my code.
I wonder if I made a mistake in my thinking. :-/
Here follows a stripped down example.

consider some dates:
$date1 = "2008-02-23";
$date2 = "2008-02-23";
(They are the same)

Suppose I need to know if date1 is smaller/equal or bigger than date2.
I often used code like:
$UTSdate1 = strtotime($date 1);
$UTSdate2 = strtotime($date 2);

if ($UTSdate1 <= $UTSdate2){
// date1 before or equal to date2
} else {
// date1 after date2
}

Good, not?

I think not after rereading the manual:

FROM: http://nl2.php.net/manual/en/function.strtotime.php
int strtotime ( string $time [, int $now ] )

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
(the number of seconds since January 1 1970 00:00:00 GMT), relative
to the timestamp given in now , or the current time if now is not
supplied.

The part "relative to the timestamp given in now" got me confused.

My question: Since the strtotime() is calculating 2 times the Unix
Time Stamp, do I risk that during execution of my script the second
is increased between the first call to strtotime() and the second,
thus making my above approach invalid?

Would this be better?
$UTSnow = time();
$UTSdate1 = strtotime($date 1,$now);
$UTSdate2 = strtotime($date 2,$now);

Should I change my code according to the second approach (using
$UTSnow)? I know chances of ticking a second are small in such a
small piece of code, but I don't want to deliver code that 'works
most of the time'.

Thanks for any insights.
Since you're specifying a specific date, instead of a relative date
string, the $now parameter won't have any effect. The output will in
any case be a standard Unix timestamp, which is the number of seconds
since Unix epoch (January 1 1970 00:00:00 GMT). In the case of the date
string "2008-02-23", the result will be the integer 1203721200, no
matter what you provide with $now.

If you had instead used, say, "+5 days", the timestamp difference would
only be measureable in microseconds, so unless you wanted extreme
precision (or you have a server that's so slow that the two instances
of strtotime() will take about 1 second to execute), I wouldn't bother
changing it.

--
Kim André Akerø
- ki******@NOSPAM betadome.com
(remove NOSPAM to contact me directly)
Jun 2 '08 #9
Erwin Moller wrote:
Captain Paralytic schreef:
On 24 Apr, 09:47, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c om>
wrote:
I think not after rereading the manual:
>
FROM:http://nl2.php.net/manual/en/function.strtotime.php
int strtotime ( string $time [, int $now ] )
>
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 (the number of seconds since January 1 1970 00:00:00
GMT), relative to the timestamp given in now , or the current
time if now is not supplied.
Hi Erwin,
in another recent thread I mentioned that the statement "The
function expects to be given a string containing a US English date
format" is contradicted later in the same page.

Hi,

Yes, I can hardly say '+5 days' is an English date format.

Since we both get confused by this entry in the manual, I suggest
somebody rewrites it. Or, of course, we both suck and should find
another job than programming PHP. ;-) Anyway, Rik cleared up my
misunderstandin g of that relative part, and I think I keep
programming PHP.
If you look further down in the manual:
time

The string to parse, according to the GNU » Date Input Formats syntax.
The text "» Date Input Formats" links to the GNU documentation on date
input formats:
http://www.gnu.org/software/tar/manu...e/tar_113.html

In the case of pure calendar dates:
http://www.gnu.org/software/tar/manu...e/tar_115.html

And, in the case of "+5 days":
http://www.gnu.org/software/tar/manu...e/tar_119.html

--
Kim André Akerø
- ki******@NOSPAM betadome.com
(remove NOSPAM to contact me directly)
Jun 2 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4658
by: Pjotr Wedersteers | last post by:
I'm using strtotime to get the timestamp for midnight. I have a statistics script for my pagecounter that displays hits since midnight. I also display the number of hours and minutes passed since midnight. $start = strtotime ('today 00:00'); $timepassed = date ('H:i', time () - $start); The weird part is at 11.30 AM it says 12 hrs and 30 minutes have passed. I tried adding GMT+1 like so: $start = strtotime ('today 00:00 GMT+1');...
1
2167
by: peabody | last post by:
I'm trying to use the strtotime() function to manage sessions. But I get the following <?php print(time() . " - " . strtotime("+1 hour")); ?> outputs: 1097380666 - 1097308800
5
4823
by: Leif K-Brooks | last post by:
PHP has a very nice strtotime function (http://php.net/strtotime) which converts a date/time in virtually any format into a timestamp. Does Python have a similar function?
7
5372
by: Rithish | last post by:
Hello. I noticed a strange thing while using strtotime() and date() functions in combination to generate from MySQL into a readable format. By default, the MySQL date field will be 0000-00-00 00:00:00 When I pass this to strtotime() to generate the timestamp, and then pass it to the date function, it generates 30-11-1999. <? print ( "<br> strtotime for '0000-00-00 00:00:00' : " . strtotime (
18
1965
by: windandwaves | last post by:
Hi Folk I love the strtotime function, because I can make a date field in a form and just tell my users to enter whatever they want and as long as it is a date, it will work. HOWEVER, this obviously does not work for the day/month vs month/day scenario. That is, in New Zealand we use day/month/year, while the function assumes it to be month/day/year.
6
1532
by: Brian Kendig | last post by:
I'm working with dates in several formats including 'yyyy MMM dd', but strtotime doesn't recognize this format and returns FALSE. Is there a direct way to convert times from this format into timestamps, other than my having to whip up a simple parsing routine? My headache is that I use strtotime in a lot of places in a lot of files, and I'd hate to have to add a wrapper everyplace I use it. I wish there were a way to extend strtotime's...
5
2516
by: cla | last post by:
I'm using this code on an application to track football schedules: ---- $season = '2005'; $basedate = strtotime('this friday', strtotime('31 August '.$season)); for($d=0;$d<=31;$d++) { $d2=strtotime("+".$d." day",$basedate); echo(date('m d Y',$d2)."\n"); } ----
5
13718
by: Chris | last post by:
I am trying to output Monday of the current week i.e. if Monday is the 8th I want to display 'Monday 8th' for any date between Monday 8-14th. I would appreciate any help, the code below is heading in the right direction but doesn't quite give me the results I am looking for. $givenday = mktime(0,0,0,10,08,2007); $Monday = strtotime("Monday this week",$givenday); echo date("j M Y H:i:s", $Monday);
3
1981
by: Anthony Smith | last post by:
So, after doing some research I finally figured that php chokes on dates past year 2525. In this web service that I use they often use 2525-05-31 as the expiration date. I want to determine how I can convert this in a format to compare it with other dates. Before I was trying this: if (strtotime($territory->expirationDate) < strtotime("2008-01-01")){ /do something }
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.