Jump to content

date problam


redarrow

Recommended Posts

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");


?>
Link to comment
Share on other sites

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:

[code]SELECT DATE_ADD(date_field, INTERVAL 1 HOUR) AS new_date FROM table[/code]

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:

[code]SELECT UNIX_TIMESTAMP(date_field) AS date_timestamp FROM table[/code]

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:

[code]$one_hour_from_now = date("format", time() + 3600);[/code]

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

4.  it's "now," not "know."  just for future reference.
Link to comment
Share on other sites

why dont this  work cheers

[code]
<?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";
}

?>
[/code]
Link to comment
Share on other sites


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

[code]

<?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";
}

?>
[/code]
Link to comment
Share on other sites

[quote author=redarrow link=topic=102641.msg407705#msg407705 date=1154477144]
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));



[/quote]

No..that is not exactly using a timestamp. One moment, I may have a solution. I will post it shortly.
Link to comment
Share on other sites

how can this be wrong

[code]
<?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";
}

?>
[/code]
Link to comment
Share on other sites

what about this then

[code]
<?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";
}

?>
[/code]
Link to comment
Share on other sites


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:

[code]<?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!';
}

?>[/code]

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.

Link to comment
Share on other sites


I cut your code version down does it make sence cheers.
[code]
<?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!';
}

?>
[/code]
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

[quote author=corbin link=topic=102641.msg407763#msg407763 date=1154484337]
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...
[/quote]

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)
Link to comment
Share on other sites

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:

[code]<?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.';
}
?>[/code]

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.
Link to comment
Share on other sites

the logic of your code will do the same as this example then and more simple to understand.

is the reason that your code use less code becouse the time() statement is a timestamp and does not need the use of strtotime within it.
[code]
<?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!';
}

?>
[/code]
Link to comment
Share on other sites

you're still ignoring the fact that you [b]NEVER WANT TO COMPARE STRING VERSIONS OF DATES.[/b]  you want to compare timestamps.  using your exact code, the following would do what you want (with both less ambiguity and more efficiency):

[code]<?php
$db_time = "12:10:44";
$get_date = '2006-01-01';

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

$timenow = time();

if ($timenow > $dbtime){
echo'Booo smelly people!';
}
else{
echo'Hooray Beer!';
}
?>[/code]

are you pulling $db_time and $get_date from the database?  if so, [b]USE MYSQL'S FUNCTIONS![/b]

your use of the last two date() functions is totally unnecessary, as this turns the date/time into a string format, and you  do not want to compare the string versions as they will not compare the DATES, they will compare the STRINGS.  that is, "August 14 2006 12:00:00" will be "less" than "June 14 2006 12:00:00" because "A" is less than "J" where strings are concerned.
Link to comment
Share on other sites

[b]I totally agree with the mysql function but i am currently learning via php only at this point sorry.[/b]

Is the concept of what i am trying to learn for collage correct thats all and thank you so much.

[code]
<?php
$database_time="01:06:30";
$database_stamp($database_time);
$time_now=date("h:i:s",time()+3600);

if($time_now>$database_stamp) {

echo " i am larger";

}else{

echo" i am smaller";

}
?>
[/code]
Link to comment
Share on other sites

your current method is still incorrect, as you're using date() to create a STRING version of the timestamp you're calculating with time() + 3600.  you shouldn't need to use date() AT ALL in this calculation, as date() is used to create a STRING version of a timestamp.

[code]$time_now=time()+3600;[/code]
Link to comment
Share on other sites


so this is correct and is the only way in php for a proper timestamp format.
[code]
<?php
$database_time="01:06:30";
$database_stamp($database_time);
$time_now=time()+3600;

if($time_now>$database_stamp) {

echo " i am larger";

}else{

echo" i am smaller";

}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.