मुझे इस प्रारूप में एक तारीख मिली है:
2009-01-01
मैं उसी तारीख को कैसे लौटूं लेकिन 1 साल पहले?
मुझे इस प्रारूप में एक तारीख मिली है:
2009-01-01
मैं उसी तारीख को कैसे लौटूं लेकिन 1 साल पहले?
जवाबों:
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
यद्यपि इस प्रश्न के उत्तर में कई स्वीकार्य उत्तर हैं, मुझे ऑब्जेक्ट subका उपयोग करने की विधि का कोई उदाहरण नहीं दिखता है \Datetime: https://www.php.net/manual/en/datetime.sub.php
इसलिए, संदर्भ के लिए, आप \DateIntervalकिसी \Datetimeऑब्जेक्ट को संशोधित करने के लिए भी उपयोग कर सकते हैं :
$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));
echo $date->format('Y-m-d');
कौन सा रिटर्न:
2008-01-01
के बारे में अधिक जानकारी के \DateIntervalलिए, प्रलेखन देखें: https://www.php.net/manual/en/class.dateinterval.php
दिनांक से 1 या किसी भी वर्ष को घटाने के लिए आप निम्न फ़ंक्शन का उपयोग कर सकते हैं।
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
और उपरोक्त उदाहरणों को देखते हुए आप निम्नलिखित का भी उपयोग कर सकते हैं
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));