date_modify를 DateTime 객체로 사용하여 php에서 현재 달의 첫 번째 날
이번 주 월요일은 다음과 같이 할 수 있습니다.
$monday = date_create()->modify('this Monday');
이번 달 1일에도 쉽게 받고 싶습니다.어떻게 하면 좋을까요?
이게 제가 쓰는 거예요.
매월 첫째 날:
date('Y-m-01');
월의 마지막 날:
date('Y-m-t');
작동하려면 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');
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');
있는 에는 '연도와 월'을 사용할 수 .date()
:
<?php
echo date('Y-m-01'); // first day of this month
echo "$year-$month-01"; // first day of a month chosen by you
이것으로 충분합니다.
$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/>';
현재 이 솔루션을 사용하고 있습니다.
$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');
내가 마주친 유일한 문제는 이상한 시간이 설정되고 있다는 것이다.검색 인터페이스의 정확한 범위가 필요했고 결국 다음과 같은 결과가 나왔습니다.
$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');
이 명령어를 사용하는 것은 미친 방법을 사용하는 것입니다.
$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));
이상입니다
php 5.2에서는 다음을 사용할 수 있습니다.
<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>
못생겼지만(상기 메서드 호출을 사용하지 않음) 다음과 같이 작동합니다.
echo 'First day of the month: ' . date('m/d/y h:i a',(strtotime('this month',strtotime(date('m/01/y')))));
다음과 같이 할 수 있습니다.
$firstday = date_create()->modify('first day January 2010');
날짜 방법을 사용하면 결과를 얻을 수 있을 것입니다.즉, date('N/D/l', mktime(0, 0, 0, 월, 일, 년);
예를들면
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
이것을 매일 cron 작업과 함께 사용하여 매월 1일에 제휴사에 메일을 보내야 하는지 확인합니다.다른 답변보다 몇 줄 더 많지만 바위처럼 단단해요.
//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;
}
이 달의 시작과 현재 달의 마지막 초의 타임스탬프입니다.00:00:00를 추가하거나 "오늘"을 참조할 수 있습니다.
다른 방법:
$startOfThisMonth = strtotime("first day of this month",strtotime("today"));
OR
$startOfThisMonth = strtotime("first day of this month 00:00:00");
$endOfThisMonth = strtotime("first day of next month",$startOfThisMonth)-1;
DateTime 객체가 바람직하지 않은 경우 대체 라이너로 이 답변을 제공합니다.
기본적으로는 현재 일수를 취득하고, 1일 줄이고, 그 일수를 취득하면(「오늘」도 자동적으로 00:00:00로 리셋 됩니다) 월의 시작이 됩니다.
$startOfMonth = strtotime("today - ".(date("j")-1)." days");
작곡기를 사용하는 경우 카본을 설치할 수 있습니다.composer require nesbot/carbon
이것은 다음과 같이 간단합니다.
use Carbon/Carbon;
$startOfMonth = Carbon::now()->startOfMonth()->toDateTime();
그 모든 특별한 php 표현들은 정신적으로first day of ...
정말 대단해요.그것들이 가끔 머릿속에서 생각나기도 해요.
그래서 저는 기본적인 날짜 추상화 몇 가지와 특정 구현 수 톤을 구축하기로 했습니다. 이 구현은 IDE에 의해 자동으로 완료됩니다.요점은 어떤 것을 찾는 것이다.맘에 들다,today
,now
,the first day of a previous month
제가 나열한 것들은 모두 날짜입니다.따라서 다음과 같은 인터페이스 또는 추상 클래스가 있습니다.ISO8601DateTime
및 그것을 실장하는 특정 날짜.
특정 케이스의 코드는 다음과 같습니다.
(new TheFirstDayOfThisMonth(new Now()))->value();
이 접근법에 대한 자세한 내용은 이 엔트리를 참조하십시오.
언급URL : https://stackoverflow.com/questions/2094797/the-first-day-of-the-current-month-in-php-using-date-modify-as-datetime-object
'source' 카테고리의 다른 글
앱을 로드하기 전에 vuex 스토어에 데이터를 로드합니다. 이 작업을 수행하려면 어떻게 해야 합니까? (0) | 2022.11.05 |
---|---|
MySQL 데이터베이스 포함 Python 3.4.0 (0) | 2022.11.05 |
DEFAULT 절에 CURRENT_TIMESTamp가 있는 TIMESTAMP 열은 왜1개밖에 없어요? (0) | 2022.10.26 |
블레이드 템플릿 뷰를 원시 HTML 문자열로 가져오는 방법 (0) | 2022.10.26 |
JavaScript에서 소수점 두 개로 숫자 형식 지정 (0) | 2022.10.26 |