为日期分配变量

发布于 2024-11-28 19:44:14 字数 853 浏览 1 评论 0原文

我需要为今天的日期分配一个特定的数字,例如 1559,并根据此计算前一天和后几天。例如,2天前是1557,昨天是1558,今天是1559,明天是1560,等等。这样,我将使用post方法创建一个分页。我的日期采用 YYYY-MM-DD 格式并存储在 mySQL 中的日期变量中。我应该如何将数字分配给日期并相应地计算日期?

顺便说一句,我的代码看起来像这样:

include ('connect.php');

    date_default_timezone_set("Etc/GMT+7");
    $timestamp= date("Y-m-d");
    $day1=1357;

    echo ('<form action="total_inf.php" method="post" name="inf_day">');
    for($i=0;$i<10;$i++)

    {
    $newdate=strtotime('-$i days',strtotime($timestamp));
    $newdate = date ( 'Y-m-d' , $newdate );

    $prevdays=$day1-$i;

    print ('<input type="submit" name="bugun" value="'.$prevdays.'">&nbsp;');

    }
    echo ('</form>');       

    $day_selected=$_POST[bugun];

    $sql1="SELECT * FROM userdata WHERE zaman='$timestamp'";
    $result1=mysql_query($sql1,$conn);

I need to assign a specific number like 1559 to today's date and calculate the previous and next days according to this. For example 2 days ago is 1557, yesterday is 1558, today is 1559 tomorrow is 1560, etc. With that, i will create a pagination for the days with post method. My date is in YYYY-MM-DD format and stored in a date variable in mySQL. What should I do to assing numbers to dates and calculate the dates accordingly?

my code looks like this btw:

include ('connect.php');

    date_default_timezone_set("Etc/GMT+7");
    $timestamp= date("Y-m-d");
    $day1=1357;

    echo ('<form action="total_inf.php" method="post" name="inf_day">');
    for($i=0;$i<10;$i++)

    {
    $newdate=strtotime('-$i days',strtotime($timestamp));
    $newdate = date ( 'Y-m-d' , $newdate );

    $prevdays=$day1-$i;

    print ('<input type="submit" name="bugun" value="'.$prevdays.'"> ');

    }
    echo ('</form>');       

    $day_selected=$_POST[bugun];

    $sql1="SELECT * FROM userdata WHERE zaman='$timestamp'";
    $result1=mysql_query($sql1,$conn);

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

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

发布评论

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

评论(1

醉城メ夜风 2024-12-05 19:44:14
$current_date = new DateTime('2011-08-10');
$current_date->modify('+1 day'); //next day
$current_date->modify('-1 day');// prev day 

$other_date = new DateTime('1970-01-01'); //another date

请参阅日期时间参考

更新:

$day1 = new DateTime();
$day1->modify('-1357 days'); 
//if you need it to be some fixed date, use $day1= new DateTime("2011-08-01");

$now_date = new DateTime();
for($i=0;$i<10;$i++){
    $date_diff = $now_date->diff($zero_date);
    $prevdays = $date_diff->format("#a")-$i;
    print ('<input type="submit" name="bugun" value="'.$prevdays.'"> ');
}

适用于 PHP >= 5.3.0。请参阅 DateInterval::format

$current_date = new DateTime('2011-08-10');
$current_date->modify('+1 day'); //next day
$current_date->modify('-1 day');// prev day 

$other_date = new DateTime('1970-01-01'); //another date

See DateTime reference

UPDATE:

$day1 = new DateTime();
$day1->modify('-1357 days'); 
//if you need it to be some fixed date, use $day1= new DateTime("2011-08-01");

$now_date = new DateTime();
for($i=0;$i<10;$i++){
    $date_diff = $now_date->diff($zero_date);
    $prevdays = $date_diff->format("#a")-$i;
    print ('<input type="submit" name="bugun" value="'.$prevdays.'"> ');
}

Works with PHP >= 5.3.0. See DateInterval::format

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