Jens14 Posted March 16, 2007 Share Posted March 16, 2007 function date_diff($str_start, $str_end) { $str_start = strtotime($str_start); $str_end = strtotime($str_end); $nseconds = $str_end - $str_start; $ndays = round($nseconds / 86400); $nseconds = $nseconds % 86400; $nhours = round($nseconds / 3600); $nseconds = $nseconds % 3600; $nminutes = round($nseconds / 60); $nseconds = $nseconds % 60; if ($ndays == 0) if($nhours == 0) if($nminutes == 0) return array($nseconds); else return array($nminutes, $nseconds); else return array($nhours, $nminutes, $nseconds); else return array($ndays, $nhours, $nminutes, $nseconds); } date_diff("2007/03/16 12:30:50", "2007/03/16 14:00:00"); This is a function that compares two dates with each other and let's see how much there is left in seconds, minutes, hours, days. I didn't know at all how to do the arrays but a friend gave me if else and return array stuff, the purpose is that first if for example the days and hours are 0 it only shows the minutes and seconds. Can anyone tell me if I'm ussing the array's wrong or/and how i get the output from these? Thanks, Jens. Link to comment https://forums.phpfreaks.com/topic/42985-showing-arrays-need-help/ Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 function date_diff($str_start, $str_end) { $str_start = strtotime($str_start); $str_end = strtotime($str_end); $nseconds = $str_end - $str_start; $ndays = round($nseconds / 86400); $nseconds = $nseconds % 86400; $nhours = round($nseconds / 3600); $nseconds = $nseconds % 3600; $nminutes = round($nseconds / 60); $nseconds = $nseconds % 60; if ($ndays == 0) if($nhours == 0) if($nminutes == 0) return array("seconds" => $nseconds); else return array("minutes" => $nminutes, "seconds" => $nseconds); else return array("hours" => $nhours, "minutes" => $nminutes, "seconds" => $nseconds); else return array("days" => $ndays, "hours" => $nhours, "minutes" => $nminutes, "seconds" => $nseconds); } $dateArr = date_diff("2007/03/16 12:30:50", "2007/03/16 14:00:00"); print_r($dateArr); // to access array elements print "Days" . $dateArr['days'] . " Hours:" . $dateArr['hours'] . " Minutes:" . $dateArr['minutes'] . " Seconds: " . $dateArr['seconds']; If you have notices to set to display and the days or hours or minutes is not returned it will throw a notice because the index of 'days' is not defined in the array to avoid that notice do this for the function function date_diff($str_start, $str_end) { $str_start = strtotime($str_start); $str_end = strtotime($str_end); $nseconds = $str_end - $str_start; $ndays = round($nseconds / 86400); $nseconds = $nseconds % 86400; $nhours = round($nseconds / 3600); $nseconds = $nseconds % 3600; $nminutes = round($nseconds / 60); $nseconds = $nseconds % 60; // this will just return 0 for days if there are none., nothing wrong that. return array("days" => $ndays, "hours" => $nhours, "minutes" => $nminutes, "seconds" => $nseconds); } $dateArr = date_diff("2007/03/16 12:30:50", "2007/03/16 14:00:00"); print_r($dateArr); // to access array elements print "Days" . $dateArr['days'] . " Hours:" . $dateArr['hours'] . " Minutes:" . $dateArr['minutes'] . " Seconds: " . $dateArr['seconds']; Link to comment https://forums.phpfreaks.com/topic/42985-showing-arrays-need-help/#findComment-208790 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.