Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php functions 1

Status
Not open for further replies.

matrixindicator

IS-IT--Management
Sep 6, 2007
418
BE
I have a delivery date in the past and I want to find out automatically how many days I count from now to that date.

Q1 : I suppose there is a function for this ?
Q2 : Is the php.net manual the best resource to look up build in functions ?
 
1. not that I know of. But if you have a string, you can use strtotime() to turn it into seconds and then just perforr a simple math operation:
Code:
$mydate="01-12-2009";
$mydays=(strtotime('NOW')-strtotime($mydate))/84600;

echo $mydays;

2. Absolutely. Its the best source for anything to do with PHP including built in functions.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
@OP

yes there is a function you can use if you have php 5.3
this should work
Code:
$then = new DateTime('2009-01-01');
echo "elapsed days: " . abs($then->diff(new DateTime)->days);

remember always to set the timezone before performing date/time maths and conversions in php
 
php version is up to date (locale and hosting) PHP 5.3.0 so OOP use should be an option, know principels of OOP but not so experienced for the moment. So with your help, a bit search, think and test I shaped this function :
Code:
	<?php
	function daysbetween($beginDate, $endDate)
	{
    // Parse dates for conversion
    $beginArry = date_parse($beginDate);
    $endArry = date_parse($endDate);
	
	$_endDate = mktime(0,0,0,$endArry["month"], $endArry["day"], $endArry["year"]);
	$_beginDate = mktime(0,0,0,$beginArry["month"], $beginArry["day"], $beginArry["year"]);

	$timestamp_diff= $_endDate-$_beginDate +1 ;
  	// how many days between those two date  	
  	$days_diff = round($timestamp_diff/86400);
  	// the last row has no previous date, simply set 0
  	if (empty($beginDate)) {$days_diff = 0;}
  	
	return $days_diff; 	  
 	}
	?>
 
if you have php5.3 then my code will work from the above post. for non php5.3 then you can use this method instead

Code:
function daysdiff($start, $end){
  date_default_timezone_set('EUROPE/LONDON');
  if (!is_number($start)) $start = strtotime($start);
  if (!is_number($end)) $end = strtotime($end);
  return ($end-$start)/86400; //convert to days from seconds
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top