यह मैं इस सवाल का सबसे लोकप्रिय रूप लगता है के बाद से यहाँ पर फेंक देंगे।
मैंने सबसे लोकप्रिय प्रकारों में से 3 पर 100 साल की तुलना की, जो मुझे PHP के लिए मिल सकते हैं और अपने परिणामों (साथ ही कार्यों) को अपने ब्लॉग पर पोस्ट किया ।
जैसा कि आप वहां देख सकते हैं , सभी 3 फंक 2 फ़ंक्शन पर सिर्फ एक मामूली अंतर के साथ अच्छी तरह से प्रीफॉर्म करते हैं। मेरे परिणामों के आधार पर मेरा सुझाव 3 फ़ंक्शन का उपयोग करना है जब तक कि आप किसी व्यक्ति के जन्मदिन पर कुछ विशिष्ट नहीं करना चाहते हैं, उस स्थिति में 1 फ़ंक्शन बिल्कुल ऐसा करने का एक सरल तरीका प्रदान करता है।
परीक्षण के साथ छोटा मुद्दा मिला, और दूसरा तरीका 2 विधि के साथ! जल्द ही ब्लॉग पर अपडेट आना! अभी के लिए, मैं नोट करूंगा, दूसरा तरीका अभी भी सबसे लोकप्रिय है जिसे मैं ऑनलाइन पाता हूं, और फिर भी अभी भी मैं सबसे अधिक अशुद्धियों को ढूंढ रहा हूं!
मेरे 100 साल की समीक्षा के बाद मेरे सुझाव:
यदि आप कुछ और बढ़ाना चाहते हैं ताकि आप जन्मदिन और इस तरह के अवसरों को शामिल कर सकें:
function getAge($date) { // Y-m-d format
$now = explode("-", date('Y-m-d'));
$dob = explode("-", $date);
$dif = $now[0] - $dob[0];
if ($dob[1] > $now[1]) { // birthday month has not hit this year
$dif -= 1;
}
elseif ($dob[1] == $now[1]) { // birthday month is this month, check day
if ($dob[2] > $now[2]) {
$dif -= 1;
}
elseif ($dob[2] == $now[2]) { // Happy Birthday!
$dif = $dif." Happy Birthday!";
};
};
return $dif;
}
getAge('1980-02-29');
लेकिन अगर आप बस उम्र जानना चाहते हैं और इससे ज्यादा कुछ नहीं, तो:
function getAge($date) { // Y-m-d format
return intval(substr(date('Ymd') - date('Ymd', strtotime($date)), 0, -4));
}
getAge('1980-02-29');
strtotimeविधि के बारे में एक महत्वपूर्ण सूचना :
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the
separator between the various components: if the separator is a slash (/),
then the American m/d/y is assumed; whereas if the separator is a dash (-)
or a dot (.), then the European d-m-y format is assumed. If, however, the
year is given in a two digit format and the separator is a dash (-, the date
string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or
DateTime::createFromFormat() when possible.
mktime()। ऐसा लगता हैstrtotime()कि php उस प्रारूप के साथ गलत है ।