Jump to content

[SOLVED] Time between two dates.


xyn

Recommended Posts

hi.

 

I've make a function to create an array of days, hours, mins between two days.

and its only returning 0. I was wondering if someone could advise me on my poor maths skills.

 

function gtime($st,$nd){
$i['start'] = strtotime($st);
$i['end'] = strtotime($nd);
if($i['start'] !== -1 && $i['end'] !== -1 ){
	if($i['end'] >= $i['start']){
		$dif = $i['end'] - $i['start'];
		if($days = intval((ceil($dif / 86400)))) $dif =$dif % 86400; 
		if($hours = intval((ceil($dif / 3600)))) $dif =$dif % 3600; 
		if($minutes = intval((ceil($dif / 60)))) $dif =$dif % 60;
		$dif = intval($dif); 
		return array('d'=>$days,'h'=>$hours,'m'=>$minutes,'s'=>$dif); 
	}
}
return false;
}

 

called via

$int = '12/11/2007';
$ago =gtime(date("d/m/Y H:i:s",strtotime($int)),date("d/m/Y H:i:s",strtotime("now")));
print $ago[d]." days ago";

Link to comment
Share on other sites

if($i['start'] !== -1 && $i['end'] !== -1 ){
why !==? use !=

 

I need to use !== in this statement.

 

I have found the problem, but am unsure of fixing it.

This is fine...

$ago =gtime(date($format[3],strtotime($int)),date($format[3],strtotime("now")));

 

However this is not...

$i['start'] = strtotime($st);
$i['end'] = strtotime($nd);

 

strtotime() seems to be removing the date.. and returning blank.

Link to comment
Share on other sites

Well i wrote my own function wich calculates the differens between 2 dates, its a bit noob function coz i guess its too big but at least it works 100%

 

// now TimeStamp returns the curent datetime in mySQL timestamp format
function nowTS() {
$newdate = date("Y-m-d H:i:s");
return $newdate;	
}

// extrdate Extracts mySQL timestamp into any date() parameter given
function extrdate($date,$parameters) {
$strtotime = strtotime($date);
$newdate = date($parameters,$strtotime);	
return $newdate;
}

// since Calculates how many time past since given time
function since($time) {
$nowtime = nowTS();

$nowyear = extrdate($nowtime,"Y");
$nowmonth = extrdate($nowtime,"m");
$nowday = extrdate($nowtime,"d");
$nowhour = extrdate($nowtime,"H");
$nowminut = extrdate($nowtime,"i");
$nowsecond = extrdate($nowtime,"s");

$timeyear = extrdate($time,"Y");
$timemonth = extrdate($time,"m");
$timeday = extrdate($time,"d");
$timehour = extrdate($time,"H");
$timeminut = extrdate($time,"i");
$timesecond = extrdate($time,"s");

$yearsince = $nowyear - $timeyear;
$monthsince = $nowmonth - $timemonth;
$daysince = $nowday - $timeday;
$hoursince = $nowhour - $timehour;
$minutsince = $nowminut - $timeminut;
$secondsince = $nowsecond - $timesecond;

/// Seconds Conf ///
if ($nowsecond < $timesecond) {
	$secondsince = 60 + $nowsecond - $timesecond;
	$minusminut = TRUE;
}
/// Minuts Conf ///
if ($minusminut == TRUE) {
	$nowminut = $nowminut - 1;

	if ($nowminut == $timeminut) {
		$minutsince =  $nowminut - $timeminut;
	}
}
if ($nowminut < $timeminut) {
	$minutsince =  60 + $nowminut - $timeminut;
	$minushour = TRUE;
}	
/// Hours Conf ///	
if ($minushour == TRUE) {
	$nowhour = $nowhour - 1;

	if ($nowhour == $timehour) {
		$hoursince =  $nowhour - $timehour;
	}
}
if ($nowhour < $timehour) {
	$hoursince =  24 + $nowhour - $timehour;
	$minusday = TRUE;
}	
/// Days Conf ///	
if ($minusday == TRUE) {
	$nowday = $nowday - 1;

	if ($nowday == $timeday) {
		$daysince =  $nowday - $timeday;
	}
}
if ($nowday < $timeday) {
	$daysince =  30 + $nowday - $timeday;
	$minusmonth = TRUE;
}	
/// Months Conf ///
if ($minusmonth == TRUE) {
	$nowmonth = $nowmonth - 1;

	if ($nowmonth == $timemonth) {
		$monthsince =  $nowmonth - $timemonth;
	}
}
if ($nowmonth < $timemonth) {
	$monthsince =  12 + $nowmonth - $timemonth;
	$minusyear = TRUE;
}	
/// Years Conf ///
if ($minusyear == TRUE) {
	$nowyear = $nowyear - 1;

	if ($nowyear == $timeyear) {
		$yearsince =  $nowyear - $timeyear;
	}
}
if ($nowyear < $timeyear) {
	$error = "The time you giving seems to be older then todays time";
}	
/////////////// end /////////////	
if (!$error){
	if (!empty($yearsince)) {
	$time = "$yearsince<i>y</i> $monthsince<i>m</i>";	
	}
	if (empty($yearsince)) {
	$time = "$monthsince<i>m</i> $daysince<i>d</i>";	
	}
	if (empty($yearsince) && empty($monthsince)) {
	$time = "$daysince<i>d</i> $hoursince<i>h</i>";	
	}
	if (empty($yearsince) && empty($monthsince) && empty($daysince)) {
	$time = "$hoursince<u>h</u> $minutsince<u>m</u>";	
	}
	if (empty($yearsince) && empty($monthsince) && empty($daysince) && empty($hoursince)) {
	$time = "$minutsince<i>m</i> $secondsince<i>s</i>";	
	}
return $time;
}
else echo $error;
}

if someone knows how to do it shorter tell me :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.