Web Hosting Talk







View Full Version : PHP Problem with date and mktime -- almost got it


GopherDesign
12-04-2008, 10:36 PM
PHP Problem with date and mktime -- almost got it

I am still looking and have found alot about it.
I know my mktime was malformed, but I am not sure what changed from one server to another. Not sure if it was the php version or perhaps the clock or something.
it did do:
date("h:i A", mktime($rs['EventBeginTime']{8} . $rs['EventBeginTime']{9}, $rs['EventBeginTime']{10} . $rs['EventBeginTime']{11}))
I got it working doing:
$hour = substr($rs['EventBeginTime'], 11, 2);
$minute = substr($rs['EventBeginTime'], 14, 2);
$second = substr($rs['EventBeginTime'], 17, 2);
$month = substr($rs['EventBeginTime'], 5, 2);
$day = substr($rs['EventBeginTime'], 8, 2);
$year = substr($rs['EventBeginTime'], 0, 4);
$StartTime = $rs['EventBeginTime'];
$fstrtim = date("h:i A",mktime ($hour,$minute,$second,$month,$day,$year));
I can now call $fstrtim and it works, but I would like to know what changed. I wonder if it is something simple becuase I do not want to have to change the code on every page.





__________________
AIM: GopherDesign
ICQ: 39 401 865
WWW: http://www.gopherdesign.com
WWW: http://www.gopherwebhosting.com

Adam-AEC
12-04-2008, 10:47 PM
What does EventBeginTime contain? Is this a date that you parsing by hand?
Does your date follow a format that can't be parsed by strptime?
I know it's not specifically an answer to your question, but it just seems like this could be the origin of your problem.





__________________
IP Address Manager for WHMCS - http://github.com/adam12/whmcs-ipman

GopherDesign
12-04-2008, 10:59 PM
I know i changed servers and upgraded php, but not sure how the time changed. Here is somehting that will answer those questions.$hour = substr($rs['EventBeginTime'], 11, 2);
$minute = substr($rs['EventBeginTime'], 14, 2);
$second = substr($rs['EventBeginTime'], 17, 2);
$month = substr($rs['EventBeginTime'], 5, 2);
$day = substr($rs['EventBeginTime'], 8, 2);
$year = substr($rs['EventBeginTime'], 0, 4);
$fstrtim = date("h:i A",mktime ($hour,$minute,$second,$month,$day,$year));
$EventBeginTime = $rs['EventBeginTime'];
$Orig = date("h:i A", mktime($rs['EventBeginTime']{8} . $rs['EventBeginTime']{9}, $rs['EventBeginTime']{10} . $rs['EventBeginTime']{11}));
echo "<br><br> <b>fstrtim output:</b> ";
echo $fstrtim;
echo "<br><br> <b>EventBeginTime output:</b> ";
echo $EventBeginTime;
echo "<br><br> <b>Orig output:</b> ";
echo $Orig;
output i got:
fstrtim output: 02:00 AM
EventBeginTime output: 2008-12-04 02:00:00
Orig output: 04:00 AM





__________________
AIM: GopherDesign
ICQ: 39 401 865
WWW: http://www.gopherdesign.com
WWW: http://www.gopherwebhosting.com

Adam-AEC
12-04-2008, 11:08 PM
It would probably help to know what you are trying to achieve?
Anyways, I'd start with parsing the time with a time function, not a string function.
PHP Code:



list($second, $minute, $hour, $day, $month, $year) = strptime($EventBeginTime, '%Y-%m-%d %H:%M:%S');











__________________
IP Address Manager for WHMCS - http://github.com/adam12/whmcs-ipman

GopherDesign
12-04-2008, 11:13 PM
This code used to work, but switched servers and it has php5 and now it does not. Gotta be a configuration setting or somehting.





__________________
AIM: GopherDesign
ICQ: 39 401 865
WWW: http://www.gopherdesign.com
WWW: http://www.gopherwebhosting.com

GopherDesign
12-04-2008, 11:16 PM
$fstrtim = date("h:i A",mktime ($hour,$minute,$second,$month,$day,$year));
that works great
but why does
date("h:i A", mktime($rs['EventBeginTime']{8} . $rs['EventBeginTime']{9}, $rs['EventBeginTime']{10} . $rs['EventBeginTime']{11}));
not?





__________________
AIM: GopherDesign
ICQ: 39 401 865
WWW: http://www.gopherdesign.com
WWW: http://www.gopherwebhosting.com

GopherDesign
12-05-2008, 01:13 AM
I just switched it all over to my updated style - must be a change in mktime





__________________
AIM: GopherDesign
ICQ: 39 401 865
WWW: http://www.gopherdesign.com
WWW: http://www.gopherwebhosting.com

azizny
12-05-2008, 02:46 AM
This is easier to do (no need to use substr or explodes):
Code:
$date = '2008-12-04 02:00:00';
echo date("h:i A", strtotime($date));
Peace,





__________________NEW: What Is Their IP - Anyone's IP a click away.
URL Tracker, Shortener, Blocker, Unblocker, Whois and More.