Solved

PHP strtotime unexpected result

Posted on 2010-09-03
11
394 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

695 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