Keep Server Online
  
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
  
or
  
 
  
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.
 |    |   
 | 
     Topic: PHP strtotime issue | 
 |   
| Author | 
  | 
 
James Blond Moderator
  
  Joined: 19 Jan 2006 Posts: 7443 Location: EU, Germany, Next to Hamburg
  | 
 Posted: Wed 01 Feb '17 12:58    Post subject: PHP strtotime issue | 
     | 
 
  | 
 
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
  | 
 Posted: Wed 01 Feb '17 18:58    Post subject:  | 
     | 
 
  | 
 
Funny bug  
 
 
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   | 
 
  | 
 
| Back to top | 
 | 
 
timo
 
 
  Joined: 03 Jun 2012 Posts: 46 Location: FI, EU
  | 
 Posted: Wed 01 Feb '17 20:37    Post subject:  | 
     | 
 
  | 
 
 	  | mraddi wrote: | 	 		  Funny bug  
 
 
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   | 	  
 
 
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 | 
 | 
 
 
 
 
 | 
 
 
 |  
 
 |  
  |   
 |