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
Share on other sites

i need to be able to calculate with dates like 2007-04-18 - 2007-04-16 to be 2 (days). To achieve this i strtotime my variables so ik kan add them to echother however i alsow want to be able to add like in my exaple 2007-04-16 + 1 day is 2007-04-17.

Link to comment
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
Share on other sites

Your way works perfectly it isnt variable for exaple if i have to add a year it wouldt work. And it needs to be in a function so that in will be quickly accessible. You have alredy helped me alot but if you have any thoughts about a function i would gladly hear it.

 

Link to comment
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
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
Share on other sites

You came for help, you got it... lots of it. The idea of this board is to fix broken code, not for us to code your entire script for you! The basis of the code is there, just read up on it and have a go yourself ;)

Link to comment
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
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.