Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP strtotime unexpected result

Posted on 2010-09-03
11
Medium Priority
?
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

618 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