Jump to content

Time function


ramli

Recommended Posts

I created a function to show me a date after i did some calculations with it. it works to show singel varables for exaple:

 

$date1 = "2007-06-16";
$date2 = "2007-06-18";
$date1 = strtotime($date1);
$date2 = strtotime($date2);

$calc = $date2 - $date1;

$dateview = TCONV($calc);
echo($dateview['day']);

 

This returns 2 days differance however when i want to calculate with a date i get strange readings

 

$date1 = "2007-06-16";
$date1 = strtotime($date1);

$date1 = $date1 + 86400; // for 1 day

$dateview = TCONV($date1);

echo($dateview['day'].$dateview['month'].$dateview['year']);

 

in this case it returns 13680.91666666775771.2307692316314.2692307692 with i dont understand wy.

It should have to return 2007-06-17.

function is below

 

//--------------------------TCONV----------------------------//
//Get date from seconds
function TCONV($dateinsecs)
{

  if($dateinsecs >= 187200) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $year = ($dateinsecs / 187200); 
  } 
  
  if($dateinsecs >= 15600) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $month = ($dateinsecs / 15600); 
  } 
  
  if($dateinsecs >= 86400) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $day = ($dateinsecs / 86400); 
  } 
  
  if($dateinsecs >= 3600) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $hour = ($dateinsecs / 3600); 
  }
  
  if($dateinsecs >= 60) 
  { 
  	$dateinsecs = floor($dateinsecs);
    $minute = ($dateinsecs / 60); 
  }
  
  if($dateinsecs >= 0) 
  { 
  	$seconds = floor($dateinsecs);
  }
  
  if(isset($year)){$time_int['year'] = $year;}
  if(isset($month)){$time_int['month'] = $month;}
  if(isset($day)){$time_int['day'] = $day;}
  if(isset($hour)){$time_int['hour'] = $hour;}
  if(isset($minute)){$time_int['minute'] = $minute;}
  if(isset($seconds)){$time_int['seconds'] = $seconds;}

  return $time_int;
}

 

I have had some problems with this bevore can somone please help me with this ?

Link to comment
https://forums.phpfreaks.com/topic/60762-time-function/
Share on other sites

<?php

/* Date Difference Calculation */

$date1 = "2007-04-18";
$date2 = "2007-04-16";

$difference = floor((strtotime($date1) - strtotime($date2)) / 24 / 60 / 60);

echo "The difference in days between {$date1} and {$date2} is {$difference} days.<br />";

/* Adding Days to a Date */

$date = "2007-04-16";
$days = 1; // Days to add to the date

$newdate = date("Y-m-d", (strtotime($date) + ($days * 24 * 60 * 60)));

echo "{$date} + {$days} day(s) = {$newdate}";

?>

 

Hope that helps ;)

Link to comment
https://forums.phpfreaks.com/topic/60762-time-function/#findComment-302780
Share on other sites

<?php

/* Date Difference Calculation */

function datediff($date1, $date2) {

return floor((strtotime($date1) - strtotime($date2)) / 24 / 60 / 60);

}

echo "The difference in days between 2007-04-18 and 2007-04-16 is ".datediff("2007-04-18", "2007-04-16")." days.<br />";

/* Adding Days to a Date */

function dateadd($date, $days, $format) {

return date($format, (strtotime($date) + ($days * 24 * 60 * 60)));

}

echo "2007-04-11 + 1 day = ".dateadd("2007-04-11", 1, "Y-m-d");

?>

 

EDIT: Forgot to mention, the last argument of dateadd() allows you to pick the format of the returned date.

Link to comment
https://forums.phpfreaks.com/topic/60762-time-function/#findComment-302803
Share on other sites

Thx for the evort i can alredy use your functions.

Am i correct i assuming this only works for processing action concerning day's. is is possible to edit this functions to include month,years,minutes,seconds and hours. I would do it myself if i knew how but im quite new at this date thing..

Link to comment
https://forums.phpfreaks.com/topic/60762-time-function/#findComment-302822
Share on other sites

Yes and i apreciate all your help and that of others. however if i could simply figure it myself i would not think of asking any of the forum members. Better yet i would make it and post it for future reference for myself and all other forum members. And im not demanding a script just requesting help on making one. If you dont want to create it berhaps a other member will. And if you find it a incorrect question please report it to the moderators. If the decide it is incorret than it is my bad. Thanks for your help again.

Link to comment
https://forums.phpfreaks.com/topic/60762-time-function/#findComment-302960
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.