php 中当前月份的第一天,使用 date_modify 作为 DateTime 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2094797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:59:44  来源:igfitidea点击:

The first day of the current month in php using date_modify as DateTime object

phpdatedatetime

提问by pihentagy

I can get the Monday of this week with:

我可以通过以下方式获得本周的星期一:

$monday = date_create()->modify('this Monday');

I would like to get with the same ease the 1st of this month. How can I achieve that?

我希望在本月 1 日也能轻松度过。我怎样才能做到这一点?

回答by Etienne Dupuis

Here is what I use.

这是我使用的。

First day of the month:

每月的第一天:

date('Y-m-01');

Last day of the month:

每月最后一天:

date('Y-m-t');

回答by John Conde

Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3). Otherwise the example above is the only way to do it:

需要 PHP 5.3 才能工作(PHP 5.3 中引入了“第一天”)。否则上面的例子是唯一的方法:

<?php
    // First day of this month
    $d = new DateTime('first day of this month');
    echo $d->format('jS, F Y');

    // First day of a specific month
    $d = new DateTime('2010-01-19');
    $d->modify('first day of this month');
    echo $d->format('jS, F Y');

    // alternatively...
    echo date_create('2010-01-19')
      ->modify('first day of this month')
      ->format('jS, F Y');

In PHP 5.4+ you can do this:

在 PHP 5.4+ 中,你可以这样做:

<?php
    // First day of this month
    echo (new DateTime('first day of this month'))->format('jS, F Y');

    echo (new DateTime('2010-01-19'))
      ->modify('first day of this month')
      ->format('jS, F Y');

If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date():

如果您更喜欢简洁的方式来做到这一点,并且已经有数字值的年份和月份,您可以使用date()

<?php
    echo date('Y-m-01'); // first day of this month
    echo date("$year-$month-01"); // first day of a month chosen by you

回答by kaleazy

This is everything you need:

这是您需要的一切:

$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());

$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());

$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());

echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';

echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';

echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';

回答by Nikola Petkanski

Currently I'm using this solution:

目前我正在使用这个解决方案:

$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');

The only issue I came upon is that strange time is being set. I needed correct range for our search interface and I ended up with this:

我遇到的唯一问题是正在设定奇怪的时间。我的搜索界面需要正确的范围,我最终得到了这个:

$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');

回答by Jorge Ricardo Sanchez Cocunubo

I use a crazy way to do this is using this command

我用一种疯狂的方法来做到这一点是使用这个命令

$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));

Thats all

就这样

回答by pihentagy

In php 5.2 you can use:

在 php 5.2 中,您可以使用:

<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>

回答by mr-sk

Ugly, (and doesn't use your method call above) but works:

丑陋,(并且不使用上面的方法调用)但有效:

echo 'First day of the month: ' . date('m/d/y h:i a',(strtotime('this month',strtotime(date('m/01/y')))));   

回答by Thomas

You can do it like this:

你可以这样做:

$firstday = date_create()->modify('first day January 2010');

回答by Jacob Nelson

using date method, we should be able to get the result. ie; date('N/D/l', mktime(0, 0, 0, month, day, year));

使用日期方法,我们应该能够得到结果。IE; date('N/D/l', mktime(0, 0, 0, 月, 日, 年));

For Example

例如

echo date('N', mktime(0, 0, 0, 7, 1, 2017));   // will return 6
echo date('D', mktime(0, 0, 0, 7, 1, 2017));   // will return Sat
echo date('l', mktime(0, 0, 0, 7, 1, 2017));   // will return Saturday

回答by Craig Edmonds

I use this with a daily cron job to check if I should send an email on the first day of any given month to my affiliates. It's a few more lines than the other answers but solid as a rock.

我将它与每日 cron 作业一起使用来检查我是否应该在任何给定月份的第一天向我的附属机构发送电子邮件。它比其他答案多几行,但坚如磐石。

//is this the first day of the month?
$date = date('Y-m-d');
$pieces = explode("-", $date);
$day = $pieces[2];

//if it's not the first day then stop
if($day != "01") {

     echo "error - it's not the first of the month today";
     exit;

}