Solved

PHP strtotime unexpected result

Posted on 2010-09-03
11
386 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
Comment Utility
that code is working fine for me using : 5.3.2.

0
 
LVL 3

Expert Comment

by:ovi_mihai
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
jrm213jrm213 -

your code returns 19691231
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
Comment Utility
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
Comment Utility
what do u need to do exactly?
0
 
LVL 17

Expert Comment

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

Expert Comment

by:gtagliani
Comment Utility
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
Comment Utility
i ment it gives u "20101105" . try it on php 4
0
 

Author Closing Comment

by:tech1984
Comment Utility
Changing previous to last fixed the issue. Thanks for the solution.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

772 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

11 Experts available now in Live!

Get 1:1 Help Now