note 105550 modified in function.date-diff by danbrown

From: Date: Fri, 26 Aug 2011 14:31:53 +0000
Subject: note 105550 modified in function.date-diff by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182756@lists.php.net to get a copy of this message
This is a very simple function to calculate the difference between two timestamp values. <?php function diff($start,$end = false) { /* * For this function, i have used the native functions of PHP. It calculates the difference between two timestamp. * * Author: Toine * * I provide more details and more function on my website */ // Checks $start and $end format (timestamp only for more simplicity and portability) if(!$end) { $end = time(); } if(!is_numeric($start) || !is_numeric($end)) { return false; } // Convert $start and $end into EN format (ISO 8601) $start = date('Y-m-d H:i:s',$start); $end = date('Y-m-d H:i:s',$end); $d_start = new DateTime($start); $d_end = new DateTime($end); $diff = $d_start->diff($d_end); // return all data $this->year = $diff->format('%y'); $this->month = $diff->format('%m'); $this->day = $diff->format('%d'); $this->hour = $diff->format('%h'); $this->min = $diff->format('%i'); $this->sec = $diff->format('%s'); return true; } /* * How use it? * * Call your php class (myClass for this example) and use the function : */ $start = strtotime('1985/02/09 13:54:17'); $end = strtotime('2012/12/12 17:30:21'); $myClass = new myClass(); $myClass->Diff($start,$end); // Display result echo 'Year: '.$myClass->Year; echo '<br />Month: '.$myClass->Month; echo '<br />Day: '.$myClass->Day; echo '<br />Hour: '.$myClass->Hour; echo '<br />Min: '.$myClass->Min; echo '<br />Sec: '.$myClass->Sec; // Display only month for all duration $month = ($myClass->Year * 12) + $myClass->Month; echo '<br />Total month: '.$month; // if you want you can use this function without $end value : $myClass->Diff($start); // Automatically the end is the current timestamp ?> --was-- This is a very simple function to calculate the difference between two timestamp values. <?php function diff($start,$end = false) { /* * For this function, i have used the native functions of PHP. It calculates the difference between two timestamp. * * Author: Toine * Website: http://www.toine.pro * * I provide more details and more function on my website */ // Checks $start and $end format (timestamp only for more simplicity and portability) if(!$end) { $end = time(); } if(!is_numeric($start) || !is_numeric($end)) { return false; } // Convert $start and $end into EN format (ISO 8601) $start = date('Y-m-d H:i:s',$start); $end = date('Y-m-d H:i:s',$end); $d_start = new DateTime($start); $d_end = new DateTime($end); $diff = $d_start->diff($d_end); // return all data $this->year = $diff->format('%y'); $this->month = $diff->format('%m'); $this->day = $diff->format('%d'); $this->hour = $diff->format('%h'); $this->min = $diff->format('%i'); $this->sec = $diff->format('%s'); return true; } /* * How use it? * * Call your php class (myClass for this example) and use the function : */ $start = strtotime('1985/02/09 13:54:17'); $end = strtotime('2012/12/12 17:30:21'); $myClass = new myClass(); $myClass->Diff($start,$end); // Display result echo 'Year: '.$myClass->Year; echo '<br />Month: '.$myClass->Month; echo '<br />Day: '.$myClass->Day; echo '<br />Hour: '.$myClass->Hour; echo '<br />Min: '.$myClass->Min; echo '<br />Sec: '.$myClass->Sec; // Display only month for all duration $month = ($myClass->Year * 12) + $myClass->Month; echo '<br />Total month: '.$month; // if you want you can use this function without $end value : $myClass->Diff($start); // Automatically the end is the current timestamp ?> http://php.net/manual/en/function.date-diff.php

« previous php.notes (#182756) next »