time() 函数数学

发布于 2024-12-11 09:52:48 字数 979 浏览 0 评论 0原文

我正在使用 time() 将日期存储在数据库中。 我有某种广告网站。 用户提交他们的广告以及他们希望展示广告的天数,例如:

标题:广告标题....

持续时间:20天

创建日期:time()(当我 在数据库上提交此广告。 )

我需要计算每个广告的纪元格式的到期日期。

exp_date = creation date + 20 days ; /// this line is hypothetical , i need correct way to calculate exp_Date
sql->set(experation date = exp_date);

我还需要知道距离到期日期还剩多少时间(以天、小时和分钟为单位),并将其显示在页面上。

    $current time = time();
    $remaining_time = $current time - $exp date ; //this line is hypothetical , i need correct way to calculate remaining_time
    echo 'remaining time : '.$remaining_time ;

最后我不想显示过期的广告(我不确定最好的方法是计算我显示广告的时间,也许我可以在 20 天后自动禁用广告状态?)

    current date = time();
    if (ads experation date > current date ) // this line is hypothetical , i need to calculate this 
    sql->update ('set ads status= 0');
    echo 'this ad has been expires ';
    exit;

I'm using time() to store date in the database.
I have some kind of advertising web site.
Users submit their ads and number of the days they want their ads to be shown for example:

title : ad title....

duration : 20 days

creation date : time() (I get the creation date by time() when I'm
submitting this ad on the database. )

I need to calculate expiration date for each ad on epoch format.

exp_date = creation date + 20 days ; /// this line is hypothetical , i need correct way to calculate exp_Date
sql->set(experation date = exp_date);

Also I need to know how much time is left to the exp date in days and hours and minutes and show it on the page.

    $current time = time();
    $remaining_time = $current time - $exp date ; //this line is hypothetical , i need correct way to calculate remaining_time
    echo 'remaining time : '.$remaining_time ;

At last I don't want to show expired ads (I'm not sure the best way to do this is to calculate time when I'm showing the ads maybe I could somehow automatically disable ads status after 20 days?)

    current date = time();
    if (ads experation date > current date ) // this line is hypothetical , i need to calculate this 
    sql->update ('set ads status= 0');
    echo 'this ad has been expires ';
    exit;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

享受孤独 2024-12-18 09:52:48

计算到期日期:

$expiration = time() + 1728000; // 1728000 = 20 * 24 * 60 * 60 (20 days)

要计算剩余时间,您应该执行类似

$currentTime = time();
$seconds = $expiration - $currentTime;

$daysLeft = (int)($seconds / 86400); // 86400 = 24*60*60 (seconds in one day)
$seconds -= $daysLeft * 86400;
$hoursLeft = (int)($seconds / 3600); // 3600 = 60*60 (seconds in one hour)
$seconds -= $hoursLeft * 3600;
$minutesLeft = (int)($seconds / 60); // seconds in one minute
$seconds -= $minutesLeft * 60;

$daysLeft$hoursLeft$minesLeft的操作$seconds 是您想要的值

To calculate the expiration date:

$expiration = time() + 1728000; // 1728000 = 20 * 24 * 60 * 60 (20 days)

To calculate the time left, you should do something like

$currentTime = time();
$seconds = $expiration - $currentTime;

$daysLeft = (int)($seconds / 86400); // 86400 = 24*60*60 (seconds in one day)
$seconds -= $daysLeft * 86400;
$hoursLeft = (int)($seconds / 3600); // 3600 = 60*60 (seconds in one hour)
$seconds -= $hoursLeft * 3600;
$minutesLeft = (int)($seconds / 60); // seconds in one minute
$seconds -= $minutesLeft * 60;

$daysLeft, $hoursLeft, $minutesLeft and $seconds are the values that you want

风情万种。 2024-12-18 09:52:48

我需要以纪元格式计算每个广告的到期日期。

你知道一分钟有多少秒吗?小时中的分钟又如何呢?一天几小时?
是什么让你无法将这些数字相乘?

我不想展示过期的广告

不要显示它们。这就是 SQL 的用途。 哪里 exp_date > unix_timestamp() 将仅选择实际广告。

I need to calculate expiration date for each ad on epoch format.

Do you happen to know how the number of seconds in one minute? What about minutes in hour? Hours in a day?
what makes you unable to multiply these numbers?

I don't want to show expired ads

don't show them. this is what SQL for. WHERE exp_date > unix_timestamp() will select only actual ads.

驱逐舰岛风号 2024-12-18 09:52:48

为什么不通过每天运行一次脚本(cron 将执行此操作)来让生活变得简单,并通过这些广告删除(或移动)它们呢?

Why not make life simple by running a script once a day (cron will do this) and do through those ads an delete (or move) them?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文