在 PHP 中为 $Date 添加天数 [英] Adding days to $Date in PHP

查看:37
本文介绍了在 PHP 中为 $Date 添加天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期作为 mySQL 查询的一部分以 2010-09-17

I have a date returned as part of a mySQL query in the form 2010-09-17

我想将变量 $Date2 设置为 $Date5 如下:

I would like to set the variables $Date2 to $Date5 as follows:

$Date2 = $Date + 1

$Date3 = $Date + 2

等等.

以便它返回 2010-09-182010-09-19 等...

so that it returns 2010-09-18, 2010-09-19 etc...

我试过了

date('Y-m-d', strtotime($Date. ' + 1 day'))

但这给了我 $Date 之前的日期.

but this gives me the date BEFORE $Date.

以Y-m-d"格式获取日期以便在其他查询中使用它们的正确方法是什么?

What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?

推荐答案

你所要做的就是使用 days 而不是 day 像这样:

All you have to do is use days instead of day like this:

<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>

而且输出正确:

2010-09-18
2010-09-19

这篇关于在 PHP 中为 $Date 添加天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆