strings not equal?? Help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
binjured
Forum Newbie
Posts: 7
Joined: Fri May 14, 2004 6:07 pm

strings not equal?? Help!

Post by binjured »

I have a really weird problem that I've had to work around for a week just because it makes literally no sense as to why it doesn't work.

A MySQL database has a variable "X" of type DateTime.
If text were to be entered into this variable as "2004-05-13 13:00:00" it would be accepted as a "DateTime" and the variable would not be overridden with the default (all 0s) right? Ok, simple enough.

Now, take the function I created to convert between timezones. You can view it below and basically get the jist of it, I don't expect you to really understand it as I made it and what I write rarely makes sense, is efficient, isn't already a built in function, etc.

Code: Select all

function est_to_pst_display() {
// get current date/time
$date = date("Y-m-d");
$time = date("H:i:s");
// explode
$newtime = explode(":",$time);
$newdate = explode("-",$date);
//get year
$year = date("Y");
//if we are going to go into negative numbers converting...
if($newtime[0] < 4) {
//get the bad number, add 24 then get rid of it
$less = $newtime[0];
$newtime[0] = $newtime[0] + 24 - $less;
//subtract a day, since we are still in yesterday
$newdate[2] -=1;
}    
else {
$newtime[0] -= 3;
}
//if our day is now 0, subtract a month then assign the last day of last month to the day
if($newdate[2] == 0) {
    $newdate[1] -=1;
//if we get 0, make it december and alter year data
    if($newdate[1] == 0) {
 $newdate[1] = 12;
 $newdate[0] -=1;
 $year -=1;
    }    
//make the date the last day of last month
    $newdate[2] = date("t",mktime(0, 0, 0, $newdate[1], 1, $year));
    }
//add a zero to the hour if needed
if($newtime[0] < 10 && strlen($newtime[0]) < 2)
$newtime[0] = "0" . $newtime[0];
//add a zero to the month if needed
if($newdate[1] < 10 && strlen($newdate[1]) < 2)
    $newdate[1] = "0" . $newdate[1];
//add a zero to the day if needed
if($newdate[2] < 10 && strlen($newdate[2]) < 2)
    $newdate[2] = "0" . $newdate[2];
//put variables back together
$new[1] = implode(":",$newtime);
$new[0] = implode("-",$newdate);
return $new[0] . ' ' . $new[1];

}

It's the last line that confuses me. It looks like it would return a valid DateTime string, with the day as xxxx-xx-xx and time as xx:xx:xx. I have compared strings, echoed comparisons, and done literally everything I can think of but the bottom line is MYSQL DOES NOT ACCEPT THIS AS A DATETIME VARIABLE and thus defaults to the one with all 0s. You can understand how this is very bad. If you know WHY 2 strings that appear identical don't read the same to php/mysql please inform me.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

est to pst time.. assuming local server time is est..

Code: Select all

date("Y-m-d H:i:s", time() - 3*60*60);
unless you're dealing with dates outside the time system's ranges (earlier than 1970, or later than somewhere in 2038 or something) that should work just fine.
binjured
Forum Newbie
Posts: 7
Joined: Fri May 14, 2004 6:07 pm

Post by binjured »

feyd wrote:est to pst time.. assuming local server time is est..

Code: Select all

date("Y-m-d H:i:s", time() - 3*60*60);
unless you're dealing with dates outside the time system's ranges (earlier than 1970, or later than somewhere in 2038 or something) that should work just fine.
I was sure there was an easier way, this did give me some practice at doing it manually though. Thank you for the code, does this automatically convert dates as well as times? I would assume it does. How does it work?

3*60sec/min*60min/hr I presume? Thanks for the info, still wish I knew why the returned string isn't correct... it confuses me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

time() returns an integer value.. number of seconds from the "epoch".. which is something like the first second of 1970. Since time() returns an integer value of seconds.. you can just do simple math for conversions to almost anything. The date function converts the integer into human readable formats using the passed value. Simple as that.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

binjured - why not post some of the string outputs your function gives?
binjured
Forum Newbie
Posts: 7
Joined: Fri May 14, 2004 6:07 pm

Post by binjured »

They are EXACT replicas of DateTime formatted strings.

Example (run right now):

"2004-05-15 09:15:44"

There are obviously no quotes on the actual one. I have checked the string for trailing spaces (trim had no effect) so I know it isn't that.

I have setup a quick page just to let you see that there literally is no difference in the strings.

http://www.binjured.com/time.php
Post Reply