Jump to content


Photo

date problam


  • Please log in to reply
25 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 11:11 PM

why does'nt the date know go one hour from know please help cheers.


<?php

// date from database.

$database_time="12:10:44";

$database_stamp=strtotime($database_time);


// date know 1 hour foward

$date_know=date("d:m:y");

$date_result=strtotime("+1 hour $date_know");


?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 01 August 2006 - 11:16 PM

ok, a few points:

1.  if you're pulling this from a database and are actually trying to add an hour to THAT date, there is a much simpler solution:

SELECT DATE_ADD(date_field, INTERVAL 1 HOUR) AS new_date FROM table

let MySQL do the work for you.

2.  if you don't want to use that method, you can at least pull the timestamp directly from the database:

SELECT UNIX_TIMESTAMP(date_field) AS date_timestamp FROM table

i'd wager that this gives far more predictable results than using strtotime() with a database date.

3.  to get the date one hour from the server's current time (rather than the date you've pulled from the database), simply go:

$one_hour_from_now = date("format", time() + 3600);

the time() + 3600 specifies the current timestamp plus 3600 seconds, which is one hour.

4.  it's "now," not "know."  just for future reference.

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 11:28 PM

why dont this  work cheers

<?php

// date from database.

$database_time="12:10:44";


// date know 1 hour foward

$date_now=date("h:i:s" , time() + 3600);



if($date_now > $database_time){

echo " i am more older then database time";

}else{

echo " i am not older then database time";
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 11:48 PM

Your comparing strings, which don't always compare they way you think they should when it comes to dates and times. Compare timestamps.

Ken

#5 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 01 August 2006 - 11:51 PM

Correct. You shouldn't store your times like that in the database. You should store them as timestamps...much easier to work with. (And more accurate)
PHP Ninja

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 11:56 PM


this is how i compere a time - time hope this is correct the output is?


<?php

// date from database.

$database_time="11:00:44";


// date now 1 hour foward

$date_now=date("h:i:s" , time() + 3600);

if($date_know-$database_time){

echo " i am more older then database time";

}else{

echo " i am not older then database time";
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 02 August 2006 - 12:00 AM

No that is wrong. You're comparing strings and that won't always work correctly.

Again, you need to store your times as timestamps in your database.
PHP Ninja

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 12:02 AM

ok i goto learn then you win caser and do it properly ok.

in my php admin what time stamp is the most used field name thanks.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 12:05 AM

that dosent make sence becouse this is a time stamp but still problams

$database_time="11:00:44";

$result_time=date("his",strtotime($database_time));



Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 02 August 2006 - 12:08 AM

that dosent make sence becouse this is a time stamp but still problams

$database_time="11:00:44";

$result_time=date("h;i;s",strtotime($databasetime));




No..that is not exactly using a timestamp. One moment, I may have a solution. I will post it shortly.
PHP Ninja

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 12:12 AM

how can this be wrong

<?php

// date from database.

$database_time="11:00:44";
$database_result=strtotime($database_time);


// date now 1 hour foward

$date_now=date("h:i:s" , time() + 3600);
$date_result=strtotime($date_now);


if($date_result-$database_result){

echo " i am more older then database time";

}else{

echo " i am not older then database time";
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 12:16 AM

what about this then

<?php

// date from database.

$database_time="11:00:44";
$database_result=strtotime($database_time);
$result1=floor($database_rusult/86400);

// date now 1 hour foward

$date_now=date("h:i:s" , time() + 3600);
$date_result=strtotime($date_now);
$result2=floor($date_result/86400);

if($result1-$result2){

echo " i am more older then database time";

}else{

echo " i am not older then database time";
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#13 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 02 August 2006 - 12:47 AM


A timestamp looks something like this: '1136146244'. And that is what you'd be inserting into the database by using the php time() function.

Anyway, if you only need to compare times...and not "dates"....then the following should work:

<?php

$db_time = "12:10:44";
$curr_time = date("h:i:s", time());
$get_date = '2006-01-01';

$timenow = strtotime($get_date." ".$curr_time);
$dbtime = strtotime($get_date." ".$db_time);

$d = date("M d, Y h:i:s", $dbtime);
$t = date("M d, Y h:i:s", $timenow);

if ($timenow > $dbtime){
echo'Booo smelly people!';
}

else{ 
echo'Hooray Beer!';
}

?>

Basically I use the date of Jan 1 2006 and the current time and your db time and throw them into a timestamp. As timestamps, they can be added, subtracted, compared...etc. If you want to echo out the $d & $t variables you can see what I was getting at.


PHP Ninja

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 01:04 AM


I cut your code version down does it make sence cheers.
<?php


// database time.

$db_time = "01:10:44";

// current time now.

$curr_time = date("h:i:s", time());


//time now time stamp

$timenow = strtotime($curr_time);


//database time stamp

$dbtime = strtotime($db_time);


// database time stamp format
$d = date("M d, Y h:i:s", $dbtime);

// time now stamp format
$t = date("M d, Y h:i:s", $timenow);

//The if conditon

if ($timenow > $dbtime){
echo'Booo smelly people!';
}

else{ 
echo'Hooray Beer!';
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#15 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 02 August 2006 - 01:41 AM

Yes, that should still work. :-) Do you only need to compare hours? And not dates?
PHP Ninja

#16 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 01:53 AM

no dates at all but the database was inseted with this date("h:i:s");
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#17 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 02 August 2006 - 02:05 AM

You could do something like

$date_raw = date(h:i:s);
$date_array = explode(":", $date_raw);
$date_num = $date_array[0] . $date_array[1] . $date_array[2];

date_num would then be a number like 120546 if it was 12:05:46

Also you could do date(his) to get the same effect... but you could do what i posted above to a date from a database...
Why doesn't anyone ever say hi, hey, or whad up world?

#18 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 02 August 2006 - 02:10 AM

You could do something like

$date_raw = date(h:i:s);
$date_array = explode(":", $date_raw);
$date_num = $date_array[0] . $date_array[1] . $date_array[2];

date_num would then be a number like 120546 if it was 12:05:46

Also you could do date(his) to get the same effect... but you could do what i posted above to a date from a database...


The problem with that method, is that although 01:30:00 PM > 12:30:00 AM....
If you join the exploded integers together and simply create '013000' & '123000', then '013000' will not be greater than '123000'....and it makes comparisons and conventional math, not possible. (At least not with desireable result). So you convert everything into timestamps so you can properly subtract hours or minutes. (See examples I provided)
PHP Ninja

#19 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 02 August 2006 - 02:19 AM

Oh wow... hmm i didnt think of that... lol
Why doesn't anyone ever say hi, hey, or whad up world?

#20 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 02 August 2006 - 04:34 AM

this entire topic is in DIRE need of an explanation of dates and times.

FIRST:

MySQL and PHP consider "timestamps" differently.  PHP's version of a timestamp is a UNIX timestamp, the number of seconds since the beginning of the unix epoch (essentially, the current date and time measured in seconds since 1970).  MySQL considers "timestamps" to be a DATETIME column type that updates everytime a record is updated.

SECOND:

when comparing dates and times, the EASIEST method (by FAR) is comparing the UNIX timestamp of each date/time.  the simple fact is, if one is greater than another, it is MORE RECENT.  that is because more seconds have passed since 1970 than the other.

to generate the current date/time in a UNIX timestamp IN PHP, you use time().  since 3600 seconds is how long an hour is, to generate a UNIX timestamp for one hour from now, you would use time() + 3600.  this much i hope is clear.

THIRD:

when pulling a date/time from a database, you can use MySQL's UNIX_TIMESTAMP() function to get an actual UNIX timestamp of the field's date/time for comparison.  this makes things much easier, as you then only have to compare the timestamp you just generated with time() and the timestamp you've just pulled from the database.

FOURTH:

using strtotime(), as you WERE, should work alright for converting a date/time into a UNIX timestamp.  this, it seems, is where you go funny.  ONCE YOU HAVE A UNIX TIMESTAMP OF EACH DATE, THAT'S IT.  THAT IS ALL YOU NEED.  there is NO point in converting them back into an actual date()-format whatsoever.

try something like this:

<?php
$db_time = "01:10:44";
$sample_date_timestamp = strtotime($db_time);
$one_hour_from_now = time() + 3600;

if ($db_time > $one_hour_from_now)
{
  echo '"database" time is more than one hour from now.';
}
else
{
  echo '"database" time is less than one hour from now.';
}
?>

however, i would URGE you to use MySQL to its full power and use their built-in date/time functions.  fudging about in PHP is utterly useless when one MySQL function will do it for you.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users