Jump to content

Recommended Posts

I need to make a php file that takes a date/time in variables like:

$userdate = “5/3/06” (This format is not set, I can change it if needs be.)
$usertime = “6” (hours. Maybe 1 to 24 as possible values)

And check the current date and time. Then compare the two and echo the difference in hours and mins.

Example: 104Hours and 45mins. (Between the current time and the user time)

This is above my head as far as the date() function goes… But maybe someone out there who is go with this sort of thing could help me. I would really appreciate it! Thanks.



Oh, ya. I found this entry by “skee”
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
$now = time();
$then = mktime(0,0,0,11,1,1984);
$diff = $now - $then;

Returns the difference (in seconds) between now and November first, 1984. The result can then be parsed through a number of functions.
[/quote]
Link to comment
https://forums.phpfreaks.com/topic/4047-need-help-with-date-time-function/
Share on other sites


Here is something I found (and changed) that outputs the difference, something like "272 days 17 hours 33 mins". Hope ya'll can use it!

[code]
<?php

$current_date = date("Y-m-d");
$current_time = date("h:i:s");
$user_date = "2006-12-1";
$user_time = "1:00:00";

function unix_diff($first_date,$first_time,$second_date,$second_time)
{

   // Author: Tone.
   // edited by Davidude
   // Date    : 15-12-2003.
  
   // Ref: Dates go in "2003-12-31".
   // Ref: Times go in "12:59:13".
   // Ref: mktime(HOUR,MIN,SEC,MONTH,DAY,YEAR).
  
   // Splits the dates into parts, to be reformatted for mktime.
   $first_date_ex = explode("-",$first_date);
   $first_time_ex = explode(":",$first_time);
   $second_date_ex = explode("-",$second_date);
   $second_time_ex = explode(":",$second_time);
  
   // makes the dates and times into unix timestamps.
   $first_unix  = mktime($first_time_ex[0], $first_time_ex[1], $first_time_ex[2], $first_date_ex[1], $first_date_ex[2], $first_date_ex[0]);
   $second_unix  = mktime($second_time_ex[0], $second_time_ex[1], $second_time_ex[2], $second_date_ex[1], $second_date_ex[2], $second_date_ex[0]);

   // Gets the difference between the two unix timestamps.
   $timediff = $first_unix-$second_unix;
              
   // Works out the days, hours, mins and secs.
   $days=intval($timediff/86400);
   $remain=$timediff%86400;
   $hours=intval($remain/3600);
   $remain=$remain%3600;
   $mins=intval($remain/60);
   $secs=$remain%60;

   // Returns a pre-formatted string. Can be chagned to an array.
   return $days . " days " . $hours . " hours " . $mins . " mins";
}

$diff = unix_diff($current_date,$current_time,$user_date,$user_time);
$diff = str_replace("-", "", $diff);

echo $diff;

?>[/code]
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.