logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: PHP strtotime issue
Author
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7374
Location: Germany, Next to Hamburg

PostPosted: Wed 01 Feb '17 12:58    Post subject: PHP strtotime issue Reply with quote

have to use PHP 5.3.3 on CentOS :/

Code:
<?php
date_default_timezone_set('Europe/Berlin');

$string1 = '1st february this year';
echo date("d.m.Y",strtotime($string1));

// result: 01.02.2017 --> OKAY

$string2 = 'last day of february this year';
echo date("d.m.Y",strtotime($string2));

// result: 31.03.2017 --> NOT OKAY


How to solve this madness!?
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 152
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Wed 01 Feb '17 18:58    Post subject: Reply with quote

Funny bug Laughing

is it possible for you to find out the first day of march and go back one day?
Code:
$string3 = 'first day of march this year';
echo date("d.m.Y",strtotime("-1 day", strtotime($string3)));


As I don't have PHP 5.3.3 available I cannot test the result myself Confused
Back to top
timo



Joined: 03 Jun 2012
Posts: 45
Location: FI, EU

PostPosted: Wed 01 Feb '17 20:37    Post subject: Reply with quote

mraddi wrote:
Funny bug Laughing

is it possible for you to find out the first day of march and go back one day?
Code:
$string3 = 'first day of march this year';
echo date("d.m.Y",strtotime("-1 day", strtotime($string3)));


As I don't have PHP 5.3.3 available I cannot test the result myself Confused


With some older PHP version (can't remember which) I had the same problem, and I tested it with this code
Code:
<?php
   $months = array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
   
   foreach ($months as $month) {
      $string = "last day of " . $month . " this year";
      $a = $month ." - " . date("j.n.Y", strtotime($string, time()));
      $b = $month ." - " . date("j.n.Y", strtotime($string, mktime(0,0,0,date("n"),1,date("Y"))));
      if ($a <> $b) $errormsg = "***"; else $errormsg = "";
      echo substr($a . str_repeat(" ",30), 0, 30) . substr($b . str_repeat(" ", 30), 0, 30) . $errormsg . "\r\n";
   }
?>

On older versions, $a produced false date under certain conditions (month had less than 31 days etc), $b was always correct date.

With PHP 7.1.2RC1 both $a and $b have correct values, here's my output:

Code:
C:\TEMP>datetest-last.php
january - 31.1.2017           january - 31.1.2017
february - 28.2.2017          february - 28.2.2017
march - 31.3.2017             march - 31.3.2017
april - 30.4.2017             april - 30.4.2017
may - 31.5.2017               may - 31.5.2017
june - 30.6.2017              june - 30.6.2017
july - 31.7.2017              july - 31.7.2017
august - 31.8.2017            august - 31.8.2017
september - 30.9.2017         september - 30.9.2017
october - 31.10.2017          october - 31.10.2017
november - 30.11.2017         november - 30.11.2017
december - 31.12.2017         december - 31.12.2017
Back to top


Reply to topic   Topic: PHP strtotime issue View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner