Solved

PHP strtotime unexpected result

Posted on 2010-09-03
11
388 Views
Last Modified: 2012-05-10
The below code echos out 20101111 instead of my expected result of 20101105.

I am running PHP 4.3.2.

Any Ideas why it is doing this?

<?

$date = date('Ymd',strtotime('+1 months',strtotime('20101001')));
echo date('Ymd',strtotime('next friday',strtotime($date)));
?>

Open in new window

0
Comment
Question by:tech1984
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 2

Expert Comment

by:gtagliani
ID: 33597644
that code is working fine for me using : 5.3.2.

0
 
LVL 3

Expert Comment

by:ovi_mihai
ID: 33597685
on later php version it gives ok time..

you can try wit singular "month"

<?php

$date = date('Ymd',strtotime('+1 month',strtotime('20101001')));
echo date('Ymd',strtotime('next friday',strtotime($date)));
?>

Open in new window

0
 

Author Comment

by:tech1984
ID: 33597722
ovi_mihai -  Changeing month the months did not work, the problem seems to be how strtotime handles next in php previous to version 4.4.

From PHP.net:
In PHP versions prior to 4.4.0, "next" is incorrectly computed as +2. A typical solution to this is to use "+1".

I need a work around for this know bug, besides upgrading php.
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33597876
well, my understanding is previous works ok

so you could try the following
<?php

$date = date('Ymd',strtotime('+1 month',strtotime('20101001')));
echo date('Ymd',strtotime('previous friday',strtotime('next friday',strtotime($date))));
?>

Open in new window

0
 

Author Comment

by:tech1984
ID: 33597914
jrm213jrm213 -

your code returns 19691231
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 33598012
try with "last" instead of "previous"
<?php



$date = date('Ymd',strtotime('+1 month',strtotime('20101001')));

echo date('Ymd',strtotime('previous friday',strtotime('next friday',strtotime($date))));

?>

Open in new window

0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33598015
what do u need to do exactly?
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33598019
sorry not sure why it posted that codeblock with the message.
0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33598071
this code does the same , but im not sure if thats what u want
<?
$date = date('Ymd',strtotime('+1 months',strtotime('20101001')));
echo date('Ymd',strtotime('+1 friday',strtotime($date)));
?>

Open in new window

0
 
LVL 2

Expert Comment

by:gtagliani
ID: 33598081
i ment it gives u "20101105" . try it on php 4
0
 

Author Closing Comment

by:tech1984
ID: 33598416
Changing previous to last fixed the issue. Thanks for the solution.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now