在 PHP 中增加日期的最简单方法?

发布于 2024-07-15 05:45:31 字数 211 浏览 7 评论 0原文

假设我有一个字符串 "2007-02-28",我可以编写将其转换为 "2007-03-01" 的最简单的代码是什么? 现在我只是使用 strtotime(),然后添加 24*60*60,然后使用 date(),但只是想知道是否有一种更干净、更简单或更聪明的方法可以做到这一点。

Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.

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

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

发布评论

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

评论(9

爺獨霸怡葒院 2024-07-22 05:45:32

添加 86400 更干净、更简单。:)

高科技方法是这样做的:

$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');

但是,如果您在日期上进行一系列转换,而不是仅仅寻找明天,那么这实际上只是远程值得的。

It's cleaner and simpler to add 86400. :)

The high-tech way is to do:

$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');

but that's really only remotely worthwhile if you're doing, say, a sequence of transformations on the date, rather than just finding tomorrow.

江城子 2024-07-22 05:45:32

您可以在 strtotime 内部进行加法,例如

 $today="2007-02-28";
 $nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));

You can do the addition right inside strtotime, e.g.

 $today="2007-02-28";
 $nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));
短暂陪伴 2024-07-22 05:45:32

另一种方法是使用函数 mktime()。 这是非常有用的函数...

$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));

但我认为在这种情况下 strtotime() 更好...

Another way is to use function mktime(). It is very useful function...

$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));

but I think strtotime() is better in that situation...

三五鸿雁 2024-07-22 05:45:32

最简单的方法...

echo date('Y-m-d',strtotime("+1 day"));    //from today

或者从指定日期开始...

echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));

The simplest way...

echo date('Y-m-d',strtotime("+1 day"));    //from today

OR from specified date...

echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));
蓝戈者 2024-07-22 05:45:32

您好,您可以尝试下面的操作,特别是如果您是法国人,

$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));

可以使用 setlocale() 函数或下面的函数翻译成法语:

function fr_date($date){

    $date = explode(' ', $date);

    $date = str_replace('Monday','Lundi',$date);
    $date = str_replace('Tuesday','Mardi',$date);
    $date = str_replace('Wednesday','Mercredi',$date);
    $date = str_replace('Thursday','Jeudi',$date);
    $date = str_replace('Friday','Vendredi',$date);
    $date = str_replace('Saturday','Samedi',$date);
    $date = str_replace('Sunday','Dimanche',$date);

    $date = str_replace('January','Janvier',$date);
    $date = str_replace('February','Février',$date);
    $date = str_replace('March','Mars',$date);
    $date = str_replace('April','Avril',$date);
    $date = str_replace('May','Mai',$date);
    $date = str_replace('June','Juin',$date);
    $date = str_replace('July','Juillet',$date);
    $date = str_replace('August','Août',$date);
    $date = str_replace('September','Septembre',$date);
    $date = str_replace('October','Octobre',$date);
    $date = str_replace('November','Novembre',$date);
    $date = str_replace('December','Décembre',$date);

    $date = implode(' ',$date);
    return $date;

}

Hello you can try this below especially if you are french

$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));

to translate in french you can use the setlocale() function or the function below :

function fr_date($date){

    $date = explode(' ', $date);

    $date = str_replace('Monday','Lundi',$date);
    $date = str_replace('Tuesday','Mardi',$date);
    $date = str_replace('Wednesday','Mercredi',$date);
    $date = str_replace('Thursday','Jeudi',$date);
    $date = str_replace('Friday','Vendredi',$date);
    $date = str_replace('Saturday','Samedi',$date);
    $date = str_replace('Sunday','Dimanche',$date);

    $date = str_replace('January','Janvier',$date);
    $date = str_replace('February','Février',$date);
    $date = str_replace('March','Mars',$date);
    $date = str_replace('April','Avril',$date);
    $date = str_replace('May','Mai',$date);
    $date = str_replace('June','Juin',$date);
    $date = str_replace('July','Juillet',$date);
    $date = str_replace('August','Août',$date);
    $date = str_replace('September','Septembre',$date);
    $date = str_replace('October','Octobre',$date);
    $date = str_replace('November','Novembre',$date);
    $date = str_replace('December','Décembre',$date);

    $date = implode(' ',$date);
    return $date;

}
骑趴 2024-07-22 05:45:32
$your_date = strtotime("1month", strtotime(date("Y-m-d")));
 $new_date = date("Y-m-d", $your_date++);
$your_date = strtotime("1month", strtotime(date("Y-m-d")));
 $new_date = date("Y-m-d", $your_date++);
凌乱心跳 2024-07-22 05:45:32

使用 strtotime() 与日期 Formate

echo date('Y-m-d', strtotime('2007-02-28' . ' +1 day'));

use strtotime() With date Formate

echo date('Y-m-d', strtotime('2007-02-28' . ' +1 day'));
沉溺在你眼里的海 2024-07-22 05:45:32
 $early_start_date = date2sql($_POST['early_leave_date']);


        $date = new DateTime($early_start_date);
        $date->modify('+1 day');


        $date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
        $date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);

        $interval = date_diff($date_a, $date_b);
 $early_start_date = date2sql($_POST['early_leave_date']);


        $date = new DateTime($early_start_date);
        $date->modify('+1 day');


        $date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
        $date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);

        $interval = date_diff($date_a, $date_b);
攀登最高峰 2024-07-22 05:45:31

一个干净的方法使用strtotime()

$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);

会给你2007-03-01

A clean way is to use strtotime()

$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);

Will give you the 2007-03-01

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