/    Sign up×
Community /Pin to ProfileBookmark

Time difference

I’ve copied this code from another site [url]http://www.charles-reace.com/PHP_and_MySQL/Time_Difference/[/url]

What I wanted was that, say I had two databases, one called newproducts
and the other called just products

if the data in newproducts’s time difference is less than two months it would stay inside the newproducts, if not it would delete the entry and place it just into the products database.

What would I have to change in these two scripts to do it?

<?php
/**
* array timeDiff(int $t1, int $t2)
*
$t1 and $t2 must be UNIX timestamp integers, order does not matter
* returns array broken down into years/months/weeks/etc.
*
/
function timeDiff($t1, $t2)
{
if($t1 > $t2)
{
$time1 = $t2;
$time2 = $t1;
}
else
{
$time1 = $t1;
$time2 = $t2;
}
$diff = array(
‘years’ => 0,
‘months’ => 0,
‘weeks’ => 0,
‘days’ => 0,
‘hours’ => 0,
‘minutes’ => 0,
‘seconds’ =>0
);

foreach(array(‘years’,’months’,’weeks’,’days’,’hours’,’minutes’,’seconds’)
as $unit)
{
while(TRUE)
{
$next = strtotime(“+1 $unit”, $time1);
if($next < $time2)
{
$time1 = $next;
$diff[$unit]++;
}
else
{
break;
}
}
}
return($diff);
}
?>

<?php
$start = strtotime(‘2007-01-15 07:35:55’);
$end = strtotime(‘2009-11-09 13:01:00’);
$diff = timeDiff($start, $end);
$output = “The difference is:”;
foreach($diff as $unit => $value)
{
echo ” $value $unit,”;
}
$output = trim($output, ‘,’);
echo $output;
?>

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@bluestarsJul 02.2007 — Where are you reading the databases?

This probably wouldn't be something that I'd do with php, although there's no reason you wouldn't be able to. I'd just use a cron script and a MySQL client.

Finding whether the date is less than two months ago is a SQL thing (wait, are you using a SQL database?), so post that in the SQL forum.
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — From my local two databases.

It's a mysql database, both of them are.

If it is an sql thing, what do you mean. Do you mean its a code that should be written in sql or some kind of function in mysql.

Maybe reconfiguring a mysql synchroniser.
Copy linkTweet thisAlerts:
@rootJul 02.2007 — add a field in one database which has both new and products, the extra field indicates if the product should be displayed as new or just as a product.

It makes for a tidy database and less PHP code.
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — Hey, that's a great idea didn't think of it. I want to do it automatically. Through, just entering products through the dates, because we have quite alot of managing them manually will be very time consuming.
Copy linkTweet thisAlerts:
@rootJul 02.2007 — You would need to look into stored routines in MySql, I havent done it myself but adding an extra field shouldnt be too hard and if your use to using phpMyAdmin, it will be fairly simple to modify the database.

The date would be the date the record was created, eg, use PHP to insert a date automatically, insert the projected date when the flag needs changing from new to product, you have many ways of automating the process and some of them can be done in mysql too like auto insert the date... It is upto you how you decide on tackling the problem.
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — So your saying, when I enter a record in new products. I enter an automatic date, then add 60 days to current date and insert into another field. Then your saying I would use a script and cron job to check the other date, if it has been met it will move it to the product database.

Which comes back to my original question, how do you add 60 days to the date if the date always comes back as year/month/day How do you add 60 days to it.
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — No need for two date fields. Just use one date field. Otherwise, you can use a boolean field to indicate whether a product is new or not -- but I wouldn't bother. I would just determine new/old products at query time.

New products list:

Select * from products Where product_date > DATE_SUB(NOW(), INTERVAL 60 DAY);

Old products list:

Select * from products Where product_date <= DATE_SUB(NOW(), INTERVAL 60 DAY);
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — Wow, now thats a very good way.

But I don't understand the DATE_SUB(NOW(), INTERVAL 60 DAYS); because the date field will always be year/month/day so can you explain how you manipulated 60 days from that. I don't actually know what DATE_SUB and NOW() and INTERVAL means.
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — DATE_SUB subtracts the specified interval from NOW() -- which is the current date and time.
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — Do I have to define the function NOW()?
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — No, is it both a PHP function and a MySQL function.
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — *

That star does it stand for anything?
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — It means to extract "all" columns from the table.
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — So products would be the table and the product_date would be the column right?
Copy linkTweet thisAlerts:
@temp_user123Jul 02.2007 — Yep.
Copy linkTweet thisAlerts:
@DragonkaiauthorJul 02.2007 — Thanx I will do that!
×

Success!

Help @Dragonkai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...