calculating from a timestamp

Go To


Yesterday I asked another question to calculate time.

Now I realized, that it makes no sense to use a second table for my project because everything that is necessary is already given in my first table. So, for the moment I use two tables where I insert two timestamps:

$time = "INSERT INTO table2 ( a, b, c) VALUES (NOW(),NOW() + INTERVAL 14 DAY,(unix_timestamp(NOW()+ INTERVAL 14 DAY)) - unix_timestamp(NOW()))";

Like I already said, it could be much easier and the same result will be reached without a second table. Now my problem is, this is the first time I have worked with timefunctions in php. There are a lot of different functions that I do not really understand.

I think to solve my problem I can use something like:

$row = fetch_assoc()
$old_timestamp = $row->b;

$actual_time = mktime(); //which makes an actual timestamp
$seconds = '1209600'; //which is exactly the timeperiod of 14 days
$added = $old_timestamp + $seconds; //which adds the seconds to the old timestamp and should be the same as NOW() + INTERVAL 14 DAY from the VALUE of the query
$date_to_check = $added - $actual_time;
   if $date_to_check <= 0 { 
     do something }

Is this possible? I would really appreciate if there is someone who can help. thanks a lot.


For everyone who likes to know what i have in mind. Here is the code for the actual way:

$time_check = $db->query("SELECT (B <= NOW()) AS var FROM table2 WHERE x='$x'");         
            while ($row = $time_check->fetch_object()){ 
                if (($row->var != 0) && ($var1 === '0')){
                        $update_status = $db->query("UPDATE table1 SET var1='1' WHERE x='$x'");{
                            $delete_timecount = $db->query("DELETE FROM table2 WHERE x='$x'");

I would like to accomplish the same result without a second table.

2012-04-04 20:42
by bonny
It's not clear to me what you are trying to accomplish - savinger 2012-04-04 20:56
i would like to change a setting after a registration and a time of 14 day - bonny 2012-04-04 21:01


I can't really understand what you are trying to do. It appears that you are trying to recognize when 14 days have elapsed after someone's registration in order to do something.

if (time() > $old_timestamp + 1209600) {
    // It's been 14 days
    // Do something

I used time, which simply returns the current time. Do you understand what a timestamp is?

2012-04-04 21:06
by savinger
i updated my question but i think this could be the solution - bonny 2012-04-04 21:17
this seems to be wrong. the update will be executed even in case when 14 days not have passed - bonny 2012-04-04 21:36
@bonny Fixed, try it now. I think I had my operator reversed - savinger 2012-04-05 02:35