मैं PHP का उपयोग करके एक महीने में पहली और आखिरी तारीख कैसे पा सकता हूं? उदाहरण के लिए, आज 21 अप्रैल, 2010 है; मैं 1 अप्रैल, 2010 और 30 अप्रैल, 2010 को खोजना चाहता हूं।
जवाबों:
सबसे आसान तरीका उपयोग करना है 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 आपको टाइमस्टैम्प से 4-अंक वाला वर्ष देता है ('2010')m आपको एक प्रमुख शून्य ('04') के साथ टाइमस्टैम्प से संख्यात्मक महीना मिलता है।t आपको टाइमस्टैम्प के महीने में दिनों की संख्या देता है ('30')इससे आप रचनात्मक हो सकते हैं। उदाहरण के लिए, एक महीने का पहला और अंतिम दूसरा पाने के लिए:
$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 देखें ।
सरल एक
संदर्भ - 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.
उम्मीद है की वो मदद करदे।
EDIT: लुइस के उत्तर को पढ़ने के बाद, यह मेरे साथ हुआ कि आप इसे सही प्रारूप (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);
}
विशिष्ट माह और वर्ष के लिए दिनांक () निम्नानुसार प्राकृतिक भाषा के रूप में उपयोग करें
$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?
लेकिन वर्तमान माह के लिए
$first_date = date('d-m-Y',strtotime('first day of this month'));
$last_date = date('d-m-Y',strtotime('last day of this month'));
यदि आप निर्दिष्ट तिथि चर से पहला दिन और अंतिम दिन खोजना चाहते हैं तो आप नीचे इस तरह कर सकते हैं:
$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);
वर्तमान तिथि के लिए बस सरल उपयोग करें
$first_date = date('Y-m-d',strtotime('first day of this month'));
$last_date = date('Y-m-d',strtotime('last day of this month'));
सरल प्रारूप में
$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);
अगले महीने के लिए $ curMonth से $ curMonth = date ('F', strtotime ("+ 1 महीने")) बदलें ;