Jump to content

Recommended Posts

hi all,

I am trying to subtract one time from another. here is what i have....

 

In my DB i have datetime (2013-07-19 16:36:18)

$time_to_subtract = '1';$time1 = date("Y-m-d",time() - (60*60*$time_to_subtract));if ($time1 > $time_in_DB){$update = 'Hello world';}

here i am trying to take 1 hour away from the time in my DB.I have tried testing my script but i have to wait over 24 hours to see the outcome.

any help?

Link to post
Share on other sites

I'd start with your $time var. It doesn't look like someting you can subtract when you echo it.

 

Edit:

 

You need to focus on UNIX time. Reread the PHP link. You'll see it.

Edited by niche
Link to post
Share on other sites

strtotime("2013-07-19 16:36:18");

 

This will return a Unix timestamp. Timestamps are given in seconds. From this timestamp you can subtract 60*60 seconds and create a timestamp one hour earlier than your previous time. With that, you can use date() to convert the new date to any format you like.

 

With new versions of PHP, you can also use the DateTime class.

Link to post
Share on other sites

westman needs to know that his $time1 isn't going to produce the timestamp he might think.

Link to post
Share on other sites

Does it? It's a simple little script. Please tell us.

Edited by niche
Link to post
Share on other sites

One problem is that $time1, $time_to_subtract and $time_in_DB are all strings.

String comparison works much different than number comparison.

If you want to do a test that takes less time, just subtract minutes instead of hours. (instead of 60*60, just try 60)

 

Since date() turns your timestamp back into a string, we don't want it.

$time_in_DB = strtotime($row['date']); // Database time is now an integer timestamp$time_to_subtract = 1; // No quotation marks because this is a number, not a string$time1 = time() - (60*60*$time_to_subtract); // No need for date() because that would turn your number back into a string, which we don't want. // The rest is just fineif ($time1 > $time_in_DB){$update = 'Hello world';}

Just to save some processing time, you can do the calculation yourself rather than make the computer do it:

Instead of 60*60, use 3600

  • Like 2
Link to post
Share on other sites

Ingolme, thank you so much for explaining each line, it's so helpful and has saved a lot of time.

 

one last question...

if

$time1 = time() - (60*60*$time_to_subtract);

works for 1 hour

 

can i use

$time1 = time() - (60*60*24*$time_to_subtract);

to last for 1 or 2 days?

Link to post
Share on other sites

Yes, that would work for days and if you multiply by seven it would be weeks, but like I said, better do the calculation yourself and put the result in your code. A day is 86400 seconds.

  • Like 1
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...