获取上个月的日期范围

发布于 2024-12-23 22:26:28 字数 96 浏览 2 评论 0原文

我需要帮助以以下格式获取前几个月的完整日期范围:Ymd

我已成功获取“本”月的完整日期范围,但无法获取“前”月的完整日期范围。

非常感谢任何帮助!

I need help getting the previous months full date range in the following format: Y-m-d

I have successfully been able to get "this" months full date range but not the "previous" months full date range.

Any help is greatly appreciated!

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

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

发布评论

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

评论(4

我ぃ本無心為│何有愛 2024-12-30 22:26:28

这可以正确完成工作:

echo date('Y-m-01 - Y-m-t', strtotime('previous month'));

这是证明:http://ideone.com/L82ZW

This gets the job done correctly:

echo date('Y-m-01 - Y-m-t', strtotime('previous month'));

Here is the proof: http://ideone.com/L82ZW

嘿嘿嘿 2024-12-30 22:26:28
    $last_month_first_day=strtotime('first day of last month');
    $no_of_days=date('t',$last_month_first_day);
    $date_value=$last_month_first_day;
    for($i=0;$i<$no_of_days;$i++)
    {
        echo date('Y-m-d',$date_value)."<br>";
        $date_value=strtotime("+1 day",$date_value);
    }

此代码将打印您想要的内容..

第一次日期:

echo date('Y-m-d',strtotime('first day of last month'));

最后一次日期:

echo date('Y-m-d',strtotime('last day of last month'));
    $last_month_first_day=strtotime('first day of last month');
    $no_of_days=date('t',$last_month_first_day);
    $date_value=$last_month_first_day;
    for($i=0;$i<$no_of_days;$i++)
    {
        echo date('Y-m-d',$date_value)."<br>";
        $date_value=strtotime("+1 day",$date_value);
    }

This code will print what you want..

First Date:

echo date('Y-m-d',strtotime('first day of last month'));

Last date:

echo date('Y-m-d',strtotime('last day of last month'));
痴者 2024-12-30 22:26:28

你可以这样做:

$month = 2;
$lastday = mktime(0, 0, 0, $month+1, 0, 2012);
$firstday = mktime(0, 0, 0, $month, 1, 2012);

$end = date("Y-m-d", $lastday);
$start = date("Y-m-d", $firstday);

任何给定月份的最后一天都可以表示为下个月的“0”日。
http://www.php.net/manual/en/function.mktime.php

You could do something like this:

$month = 2;
$lastday = mktime(0, 0, 0, $month+1, 0, 2012);
$firstday = mktime(0, 0, 0, $month, 1, 2012);

$end = date("Y-m-d", $lastday);
$start = date("Y-m-d", $firstday);

The last day of any given month can be expressed as the "0" day of the next month.
http://www.php.net/manual/en/function.mktime.php

一城柳絮吹成雪 2024-12-30 22:26:28
// works with PHP 5.3 or later
$today = new DateTime();
$thisMonthFirstDay = $today->setDate($today->format('Y'), $today->format('m'), 1);
$previousMonthLastDay = $thisMonthFirstDay->sub(new DateInterval('P1D')); // substract 1 day

$daysInLastMonth = $previousMonthLastDay->format('d');

for($i=1; $i<=$daysInLastMonth; $i++) {
    $num = ($i < 10) ?'0'.$i :$i; // add zero in front if < 10
    echo $previousMonthLastDay->format('Y-m-') . $num. "\n";
} 
// works with PHP 5.3 or later
$today = new DateTime();
$thisMonthFirstDay = $today->setDate($today->format('Y'), $today->format('m'), 1);
$previousMonthLastDay = $thisMonthFirstDay->sub(new DateInterval('P1D')); // substract 1 day

$daysInLastMonth = $previousMonthLastDay->format('d');

for($i=1; $i<=$daysInLastMonth; $i++) {
    $num = ($i < 10) ?'0'.$i :$i; // add zero in front if < 10
    echo $previousMonthLastDay->format('Y-m-') . $num. "\n";
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文