Jump to content

[SOLVED] Timestamp Difference in Minutes


everurssantosh

Recommended Posts

Hi ,

 

I am working on a project where I need to find out difference between two dates in minutes.

 

First souce is stored in mysql timestamp field and looks like : ]"2008-09-20 06:30:46"

[/b

Second Souce is a time which I generate at time of executing the cod in PHP looks like : 2008-09-20 08:30:22 which I generate using the follwiing code in PHP

$date_now =  date("Y-m-d H:i:s ") ;

 

I have tried lot of functions using PHP but I am not able to find the difference between two inputs in minutes.

Kindly HELP

 

 

Thanks

Santosh

 

Link to comment
https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/
Share on other sites

What you could do is put the minutes into a variable like so:

 

$minutes1 = date("i");

$minutes2 = date("i"); //this would be the date now.

Then compare.

$difference = $minutes2 - $minutes1;

 

It might not be the complete solution but I'm sure you could find a way to finish it off.

 

I hope that helps.

 

 

Simple. Compare timestamps (seconds), knowing that 60 seconds is a minute:

 

<?php
$db_date = '2008-09-20 06:30:46';
$db_stamp = strtotime($db_date);
$diff = round((time() - $db_stamp) / 60);
?>

 

You can also do it directly in the MySQL query, I just can't remember how.

Server time by setting timezone: 01:56:04 pm (20)

 

 

I got this while accessing your page...

 

but my query is still not solved.

 

i need the difference in minutes..

if it is 2 days 3 hours 19 mins, then i need it in minutes...

 

pleaes help

I have a function that you might like to use:

 

function time_since($time){

		$now = mktime();
		$seconds_since = $now - $time;

		if($seconds_since < 60){
			return $seconds_since.' seconds ago';
		}
		else if($seconds_since > 59 && $seconds_since < 120){
			return '1 minute ago';
		}
		else if($seconds_since > 119 && $seconds_since < 3600){
			$minutes = round($seconds_since / 60);
			return $minutes.' minutes ago';
		}
		else if($seconds_since > 3599 && $seconds_since < 86400){
			$hours = round($seconds_since / 3600);
			if($hours == 1){
				return "1 hour ago";
			}
			else{
				return $hours.' hours ago';
			}
		}
		else if($seconds_since > 86400 && $seconds_since < 604800){
			$days = round($seconds_since/86400);
			if($days == 1){
				return "1 day ago";
			}
			else{
				return $days.' days ago';
			}
		}
		else if($seconds_since > 604799){
			$weeks = round($seconds_since/604800);
			if($weeks == 1){
				return "1 week ago";
			}
			else{
				return $weeks.' weeks ago';
			}
		}
	}

 

Not the best but does the job for me.

Hi,

 

I have seen three replies but I am not able to get the correct code yet.

 

I have one time in database with mysql timestamp in a format :2008-09-20 07:30:46

The other comparision parameter is the current time of the server.

 

I need the difference in minutes... the functions given doesnt take care of the date or year ... or the 24 hours date format.. .

 

pleaes help

 

Thanks

Santosh

Go with Mchl's query solution, if it works.

 

What's wrong with my code, though?

 

<?php
$db_date = '2008-09-20 15:00:00';
$db_stamp = strtotime($db_date);
$diff = round((time() - $db_stamp) / 60);
echo "$diff minutes";
?>

 

Outputs

 

75 minutes

 

(It's 16:15 here).

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.