note 40321 added to function.date

From: Date: Sat, 28 Feb 2004 20:53:17 +0000
Subject: note 40321 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-65958@lists.php.net to get a copy of this message
I've been looking for a function, which could count years, months, days, hours and minutes since some date till now, but I couldn't find any. So I wrote one, using the date(). Seems to work OK. <?php function from2now($year, $month, $day, $hour, $minute) { // common $todayMonth = date(n); // current month $todayDay = date(j); // current day $todayHour = date(G); // current hour $todayMinute = date(i); // current minute /* $year - given start year $month - given start month $day - given start day $hour - given start hour $minute - given start minute */ ### count years $monthday = date(nj); $link = $month.$day; $datepoint = $link; if ($monthday < $datepoint) $years = date(Y) - $year - 1; else $years = date(Y) - $year; // proper form of words (year, years) if ($years < 1) $yearWord = ''; elseif ($years == 1) $yearWord = 'year'; elseif ($years > 1) $yearWord = 'years'; ### count months if ($todayMonth > $month) { if ($todayDay < $day) $months = $todayMonth - $month -1; else $months = $todayMonth - $month; } else { $between = $month - $todayMonth; $plus = 12 - $month; if ($todayDay < $day) $months = $month + $plus - $between -1; else $months = $month + $plus - $between; } // again, proper form of words (month, months) if ($months == 1) $monthWord = 'month'; elseif ($months > 1) $monthWord = 'months'; ### count days if ($todayDay > $day) { if ($todayHour < $hour) $days = $todayDay - $day -1; else $days = $todayDay - $day; } else { $between = $day - $todayDay; $daysToEndOfMonth = date(t) - $day; if (date(t) == 28) $plus = $daysToEndOfMonth; if (date(t) == 29) $plus = $daysToEndOfMonth; if (date(t) == 30) $plus = $daysToEndOfMonth; if (date(t) == 31) $plus = $daysToEndOfMonth; if ($todayHour < $hour) $days = $day + $plus - $between - 1; else $days = $day + $plus - $between; } // words... if ($days == 1) $dayWord = 'day'; else $dayWord = 'days'; ### count hours if ($todayHour > $hour) { if ($todayMinute < $minute) $hours = $todayHour - $hour - 1; else $hours = $todayHour - $hour; } else { $between = $hour - $todayHour; $plus = 24 - $hour; if ($todayMinute < $minute) $hours = $hour + $plus - $between - 1; else $hours = $hour + $plus - $between; } // ... if ($hours == 1) $hourWord = 'hour'; if ($hours > 1) $hourWord = 'hours'; ### count minutes if ($todayMinute > $minute) { $minutes = $todayMinute - $minute; } else { $between = $minute - $todayMinute; $plus = 60 - $minute; $minutes = $minute + $plus - $between; } if ($minutes == 60) $minutes = 0; if ($minutes == 1) $minuteWord = 'minute'; elseif ($minutes > 1) $minuteWord = 'minutes'; if ($years == 0) { $years = ''; $yearWord = ''; } if ($months == 0) { $months = ''; $monthWord = ''; } if ($days == 0) { $days = ''; $dayWord = ''; } if ($hours == 0) { $hours = ''; $hourWord = ''; } if ($minutes == 0) { $minutes = ''; $minuteWord = ''; } ### echo result echo $years.' '.$yearWord.' '.$months.' '.$monthWord.' '.$days.' '.$dayWord.' '.$hours.' '.$hourWord.' '.$minutes.' '.$minuteWord.''; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40321 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40321&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40321&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65958) next »