Welcome, Guest
Username: Password: Remember me

TOPIC: Coding Error in dates.php

Coding Error in dates.php 19 Sep 2016 15:44 #14556

  • koenvdz
  • koenvdz's Avatar
  • Offline
  • Junior Member
  • Posts: 24
  • Thank you received: 2
  • Karma: 0
Hi Admin,

I was using the timeFromFormat method and getting a wrong result, So I checked the methods in dates.php (so I am using it wrong or there really is an error in the method)
ex:
input : StartDate & StartTime: string(10) "2016-10-27" & string(8) "16:09:00
call to method : $startunix = GuidemanHelperDates::timeFromFormat($data->start_date . ' ' . $data->start_time, '%Y-%m-%d %H:%M:%S');
Result:
In explodeDate() ==> Pos: array(7) { ["minute"]=> bool(false) ["year2"]=> bool(false) ["second"]=> bool(false) ["year4"]=> int(1) ["month"]=> int(4) ["day"]=> int(7) ["hour"]=> int(10) }
array(6) { ["year"]=> string(4) "2016" ["month"]=> string(2) "10" ["day"]=> string(2) "27" ["hour"]=> string(2) "16" ["minute"]=> string(2) "00" ["second"]=> string(2) "00" }

When you change the following code in the explodeDate() method:
//Prepare the search depending on attempted format
$pos = array();
$pos = strpos($format, "Y");
$pos = strpos($format, "y");
$pos = strpos($format, "m");
$pos = strpos($format, "d");

$pos = strpos($format, "H");
$pos = strpos($format, "i");
$pos = strpos($format, "s");
to :
//Prepare the search depending on attempted format
$pos = array();
$pos = strpos($format, "Y");
$pos = strpos($format, "y");
$pos = strpos($format, "m");
$pos = strpos($format, "d");

$pos = strpos($format, "H");
$pos = strpos($format, "M");
$pos = strpos($format, "S");

it works fine.
Result =
Pos: array(7) { ["year2"]=> bool(false) ["year4"]=> int(1) ["month"]=> int(4) ["day"]=> int(7) ["hour"]=> int(10) ["minute"]=> int(13) ["second"]=> int(16) }
array(6) { ["year"]=> string(4) "2016" ["month"]=> string(2) "10" ["day"]=> string(2) "27" ["hour"]=> string(2) "16" ["minute"]=> string(2) "09" ["second"]=> string(2) "00" }

Kind Regards,
Koen
Koen
The administrator has disabled public write access.

Coding Error in dates.php 19 Sep 2016 17:52 #14558

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
M is the format for a short textual representation of a month, three letters, see php.net/manual/en/function.date.php

You should not format with %M, but with %i (correct php format)
The parameter declaration in timeFromFormat and explodeDate is wrong..

So: $startunix = GuidemanHelperDates::timeFromFormat($data->start_date . ' ' . $data->start_time, '%Y-%m-%d %H:%i:%s');
-> lowercase s
Last Edit: 19 Sep 2016 17:53 by Romkabouter.
The administrator has disabled public write access.
The following user(s) said Thank You: koenvdz

Coding Error in dates.php 19 Sep 2016 18:32 #14559

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Exact.
The minutes are " i " in php.

Also, you can remove the % because this is old stuff. You don't need them at all.
Look at your component, we are not using that anymore.
and for the seconds, the 's' must be lowercase.
Coding is now a piece of cake
Last Edit: 19 Sep 2016 18:33 by admin.
The administrator has disabled public write access.
The following user(s) said Thank You: koenvdz

Coding Error in dates.php 19 Sep 2016 19:02 #14560

  • koenvdz
  • koenvdz's Avatar
  • Offline
  • Junior Member
  • Posts: 24
  • Thank you received: 2
  • Karma: 0
Hi Romkabouter and Admin, you are both right, I got misled by the param description in the comments of the method, I tried with %i and %s and it does not give a result (all zeroes), but if you drop the % like admin suggested it works fine. Thanks.
Koen
The administrator has disabled public write access.
Time to create page: 0.093 seconds

Get Started