यहाँ मैं अब तक क्या कर लिया है:प्रारूपित स्ट्रिंग के रूप में दो दिनों के बीच अंतर की गणना कैसे करें?
/**
* Parse a duration between 2 date/times in seconds
* and to convert that duration into a formatted string
*
* @param integer $time_start start time in seconds
* @param integer $time_end end time in seconds
* @param string $format like the php strftime formatting uses %y %m %w %d %h or %i.
* @param boolean $chop chop off sections that have 0 values
*/
public static function FormatDateDiff($time_start = 0, $time_end = 0, $format = "%s", $chop = false) {
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
list($year_start,$month_start,$day_start) = explode('-',date('Y-m-d',$time_start));
list($year_end,$month_end,$day_end) = explode('-',date('Y-m-d',$time_end));
$years = $year_end - $year_start;
$months = $month_end - $month_start;
$days = $day_start - $day_end;
$weeks = 0;
$hours = 0;
$mins = 0;
$secs = 0;
if(mktime(0,0,0,$month_end,$day_end) < mktime(0,0,0,$month_start,$day_start)) {
$years -= 1;
}
if($days < 0) {
$months -= 1;
$days += 30; // this is an approximation...not sure how to figure this out
}
if($months < 0) $months += 12;
if(strpos($format, '%y')===false) {
$months += $years * 12;
}
if(strpos($format, '%w')!==false) {
$weeks = floor($days/7);
$days %= 7;
}
echo date('Y-m-d',$time_start).' to '.date('Y-m-d',$time_end).": {$years}y {$months}m {$weeks}w {$days}d<br/>";
}
मैं गणित सही पाने के लिए नहीं कर पा रहे (यह अधूरा और गलत है)। इसे निष्क्रिय रूप से विभाजित करने से लीप वर्षों और महीनों की अलग-अलग लंबाई के कारण काम नहीं होगा।
तर्क स्ट्रिंग के आधार पर तर्क को भी बदलने की आवश्यकता है। उदाहरण के लिए, 04-फरवरी -2010 को 28-जून -2011 (यूनिक्स टाइमस्टैम्प के रूप में) प्रारूप स्ट्रिंग %y year %m month %d day
के साथ आउटपुट 1 year 4 month 24 day
आउटपुट होना चाहिए, लेकिन यदि %y year
छोड़ा गया है तो उसे महीने में 12 महीने जोड़ना होगा, यानी आउटपुट 16 month 24 day
होना चाहिए।
को समय भी संभालना चाहिए ... लेकिन मुझे अभी तक यह नहीं मिला है। इन date_diff समाधान के
कोई नहीं सप्ताह संभाल। और मुझे नहीं पता कि मैं इसे date_diff
में कैसे हैक कर सकता हूं, इसलिए यह वास्तव में मेरे लिए एक समाधान नहीं है।
इसके अलावा, $diff->format
जो मैंने पूछा है वह नहीं करता है ... "बड़ी इकाइयां" छोड़े जाने पर कुल महीनों और दिनों को देने के लिए। उदाहरण:
>>> $start = new DateTime('04-Feb-2010')
>>> $end = new DateTime('28-Jun-2011')
>>> $diff = $start->diff($end)
>>> $diff->format('%m months, %d days')
'4 months, 24 days'
16 months, 24 days
होना चाहिए, जैसा कि मैंने पहले बताया था। इससे पहले कि आप इसे पूरी तरह समझ सकें, कृपया मेरे प्रश्न को डुप्ली के रूप में बंद करने के लिए इतनी जल्दी रुकना बंद करें। यदि अन्य प्रश्नों के समाधान को हल करने के लिए tweaked किया जा सकता है, ठीक है, लेकिन कृपया बताएं कि कैसे, क्योंकि मुझे यह नहीं मिलता है।
स्पष्ट है कि,
- अगर
%y
छोड़ दिया जाता है, साल महीने - अगर
%m
छोड़ दिया जाता है में उपलब्ध हो जाएगा, महीनों - अगर
%w
छोड़ दिया जाता है, सप्ताह के दिनों में उपलब्ध हो जाएगा %h
छोड़ा गया है, घंटों को- में घुमाया जाना चाहिएछोड़ दिया जाता है, मिनट सेकंड
"छोटी इकाइयों" छोड़े गए हैं, अगले सबसे बड़ी इकाई गोल या फिदा जहां यह समझ में आता है किया जा सकता है में उपलब्ध हो जाएगा।
आप ऊपर पीएचपी 5.3.0 या है, तो यह आपकी मदद कर सकता है: [DateInterval :: प्रारूप] (http://www.php.net/manual/en/dateinterval.format.php) – drew010
डुप्लिकेट [इस पोस्ट] (http: // stackoverflow।कॉम/प्रश्न/676824/कैसे-गणना-अंतर-बीच-दो-तिथियों-उपयोग-php) – bowlerae
@ ड्रू010: नहीं ... जो सप्ताहों को संभाल नहीं करता है। – mpen