Link to home
Start Free TrialLog in
Avatar of tech1984
tech1984

asked on

PHP strtotime unexpected result

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

Avatar of gtagliani
gtagliani

that code is working fine for me using : 5.3.2.

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

Avatar of tech1984

ASKER

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

jrm213jrm213 -

your code returns 19691231
ASKER CERTIFIED SOLUTION
Avatar of jrm213jrm213
jrm213jrm213
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what do u need to do exactly?
sorry not sure why it posted that codeblock with the message.
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

i ment it gives u "20101105" . try it on php 4
Changing previous to last fixed the issue. Thanks for the solution.