PHP를 사용하여 한 달의 첫 번째 날짜와 마지막 날짜를 어떻게 찾을 수 있습니까?
PHP를 사용하여 한 달의 첫 번째 날짜와 마지막 날짜를 어떻게 찾을 수 있습니까? 예를 들어 오늘은 2010 년 4 월 21 일입니다. 2010 년 4 월 1 일과 2010 년 4 월 30 일을 찾고 싶습니다.
가장 쉬운 방법은를 사용하는 것입니다.이 방법 date을 사용하면 하드 코딩 된 값을 타임 스탬프에서 추출한 값과 혼합 할 수 있습니다. 타임 스탬프를 제공하지 않으면 현재 날짜와 시간을 가정합니다.
// Current timestamp is assumed, so these find first and last day of THIS month
$first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day
$last_day_this_month = date('m-t-Y');
// With timestamp, this gets last day of April 2010
$last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));
date()주어진 문자열 'm-t-Y'에서 특정 기호를 검색하여 타임 스탬프의 값으로 바꿉니다. 따라서 이러한 기호를 사용하여 타임 스탬프에서 원하는 값과 서식을 추출 할 수 있습니다. 위의 예에서 :
Y타임 스탬프 ( '2010')에서 4 자리 연도를 제공합니다.m타임 스탬프에서 숫자로 된 월을 제공하며 앞에 0이 붙습니다 ( '04')t타임 스탬프 월 ( '30')의 일 수를 제공합니다.
이것으로 창의력을 발휘할 수 있습니다. 예를 들어, 한 달의 1 초와 마지막 초를 가져 오려면 :
$timestamp = strtotime('February 2012');
$first_second = date('m-01-Y 00:00:00', $timestamp);
$last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!
기타 기호 및 자세한 내용 은 http://php.net/manual/en/function.date.php 를 참조 하십시오.
간단한 것
- Y-연도의 전체 숫자 표현, 4 자리
- m-앞에 0이있는 월의 숫자 표현
- t-주어진 달의 일수
참조-http: //www.php.net/manual/en/function.date.php
<?php
echo 'First Date = ' . date('Y-m-01') . '<br />';
echo 'Last Date = ' . date('Y-m-t') . '<br />';
?>
날짜 함수를 사용하여 한 달에 몇 일이 있는지 확인할 수 있습니다.
// Get the timestamp for the date/month in question.
$ts = strtotime('April 2010');
echo date('t', $ts);
// Result: 30, therefore, April 30, 2010 is the last day of that month.
도움이되기를 바랍니다.
편집 : Luis의 답변을 읽은 후 올바른 형식 (YY-mm-dd)으로 원할 수 있습니다. 명백 할 수 있지만 언급해도 괜찮습니다.
// After the above code
echo date('Y-m-t', $ts);
그러면 해당 월의 마지막 날이 제공됩니다.
function lastday($month = '', $year = '') {
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
그리고 첫날 :
function firstDay($month = '', $year = '')
{
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
return date('Y-m-d', $result);
}
For specific month and year use date() as natural language as following
$first_date = date('d-m-Y',strtotime('first day of april 2010'));
$last_date = date('d-m-Y',strtotime('last day of april 2010'));
// Isn't it simple way?
but for Current month
$first_date = date('d-m-Y',strtotime('first day of this month'));
$last_date = date('d-m-Y',strtotime('last day of this month'));
If you want to find the first day and last day from the specified date variable then you can do this like below:
$date = '2012-02-12';//your given date
$first_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", first day of this month");
echo $first_date = date("Y-m-d",$first_date_find);
$last_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", last day of this month");
echo $last_date = date("Y-m-d",$last_date_find);
For the current date just simple use this
$first_date = date('Y-m-d',strtotime('first day of this month'));
$last_date = date('Y-m-d',strtotime('last day of this month'));
To get the first and last date of Last Month;
$dateBegin = strtotime("first day of last month");
$dateEnd = strtotime("last day of last month");
echo date("D-F-Y", $dateBegin);
echo "<br>";
echo date("D-F-Y", $dateEnd);
In simple format
$curMonth = date('F');
$curYear = date('Y');
$timestamp = strtotime($curMonth.' '.$curYear);
$first_second = date('Y-m-01 00:00:00', $timestamp);
$last_second = date('Y-m-t 12:59:59', $timestamp);
For next month change $curMonth to $curMonth = date('F',strtotime("+1 months"));
$month=01;
$year=2015;
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo $num;
display 31 last day of date
'Program Club' 카테고리의 다른 글
| Mixpanel 오류 :“mixpanel”개체가 초기화되지 않았습니다. (0) | 2020.10.17 |
|---|---|
| 호스트 localhost 포트 22에 연결 : 연결이 거부되었습니다. (0) | 2020.10.17 |
| 도메인 및 하위 도메인에 대한 자체 서명 된 인증서 만들기-NET :: ERR_CERT_COMMON_NAME_INVALID (0) | 2020.10.16 |
| Python에서 개체 목록 만들기 (0) | 2020.10.16 |
| 파사드 vs. 중재자 (0) | 2020.10.16 |