मैं PHP का उपयोग करके एक महीने में पहली और आखिरी तारीख कैसे पा सकता हूं?


84

मैं PHP का उपयोग करके एक महीने में पहली और आखिरी तारीख कैसे पा सकता हूं? उदाहरण के लिए, आज 21 अप्रैल, 2010 है; मैं 1 अप्रैल, 2010 और 30 अप्रैल, 2010 को खोजना चाहता हूं।


जवाबों:


207

सबसे आसान तरीका उपयोग करना है 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 देखें ।


3
बस यहाँ जानकारी जोड़ते हुए, मुझे इस प्रारूप के साथ यूनिक्स टाइमस्टैम्प में तारीख बदलने में समस्या थी, 'mdY' के बजाय 'dmY' प्रारूप का उपयोग करके तय किया गया था .. साथ ही अंतिम दूसरा '12: 59: 59 'होगा बजाय 12 : 59: 59 '..
सैयद क़रीब

27

सरल एक

  • Y - एक वर्ष का पूर्ण संख्यात्मक प्रतिनिधित्व, 4 अंक
  • मीटर - एक महीने का संख्यात्मक प्रतिनिधित्व, अग्रणी शून्य के साथ
  • 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 />';
?>

19

एक महीने में कितने दिन हैं, यह जानने के लिए आप तिथि फ़ंक्शन का उपयोग कर सकते हैं।

// 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); 

12

यह आपको महीने का अंतिम दिन देगा:

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);
} 

6

विशिष्ट माह और वर्ष के लिए दिनांक () निम्नानुसार प्राकृतिक भाषा के रूप में उपयोग करें

$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'));

5

यदि आप निर्दिष्ट तिथि चर से पहला दिन और अंतिम दिन खोजना चाहते हैं तो आप नीचे इस तरह कर सकते हैं:

$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'));

आप तिथि ("Ymd", strtotime ($ date)) को $ तिथि के साथ बदल सकते हैं क्योंकि वे समान हैं लेकिन आपके पास कम कोड हैं।
निकोलस्कॉलमैन

2

की पहली और अंतिम तिथि प्राप्त करने के लिए 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);

2

सरल प्रारूप में

   $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 महीने")) बदलें ;


0
$month=01;
$year=2015;
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo $num;

दिनांक के अंतिम दिन 31 प्रदर्शित करें

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.