Weeks between 2 mysql formatted datetime [duplicate] - php

This question already has answers here:
MySQL week calculation between two dates
(2 answers)
Closed 10 years ago.
I am creating a Top 30 Music Chart..
Hw do I get the number of weeks between two mysql formatted datetimes? like
From: 2013-01-15 11:41:14
Current Datetime: 2013-02-25 13:41:14

Using SQL is better, but in php it'll be something like this:
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;

Subtract the times using strtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference / 604800 );

<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
You can just chunk down the seconds with "remainder" and find out the minutes (and it's remaining seconds) and so on until you hit the time-distance you want.

does the WEEK() function helps you in your query ?
I don't know Mysql very well but lets assume the following :
select WEEK(date1-date2) from mytable where name='Ken';

You can use strtotime() method, then count difference between current time and time you have in your MySQL database.

Try DATEDIFF(date1, date2) function in mysql.
DATEDIFF() will return you the no of date between two given date. Hope you can easily get the weeks from output.

Related

PHP DateTime diff returns wrong difference [duplicate]

This question already has answers here:
Using DateTime::diff() to return days between two dates
(2 answers)
Closed 4 years ago.
I calculate the difference of to queried dates. When the difference is lower than one month everything is correct. Here a example of a wrong result:
Query output:
$row['start'] = '2018-08-06';
$row['end'] = '2018-09-26';
Code:
$start = new DateTime($row['start']);
$end = new DateTime($row['end']);
$days = $start->diff($end)->format("%d");
Output:
$days = 20;
The DateTime difference is correct and the error only comes in when you are formatting the output. %d is the day of the month, not the total days in the DateInterval. These two datetimes are 1 month and 20 days apart. So %d only shows the 20 days part. %a should get you the total number of days.
A full guide to the format can be found here:
http://php.net/manual/en/dateinterval.format.php

strtotime and weird results when calculating time differences (datetime)

I've been trying at this for a bit and can't get the damn code to work.. This is my first post, I've gone through a few, tried a million different ways.. I just want to get the difference in hours, then I'm set, I'll get the rest figured out..
Right now, it's giving me unusual answers (say there's a 2 hour difference, it'll give me 14 as an answer) Pardon my coding, I haven't done this in years and have no real formal training. I'll be as thorough as possible in my comments, and thanks a LOT. Any links appreciated. I have tried a LOT. Using PHP 5.3.something, and am pulling off a Wordpress 3.7.1 database.
Thanks in advance for the help for a beginner. I want to display "Updated x hours ago". Once I have the darned thing displaying the correct result, I'll figure the rest out.
//This is the current date, putting it into strtotime so everything is in the same format. It displays accurately.
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDateHour = date("H", strtotime($currentDate . $currentTime));
// This is the date I'm pulling from the database, it only displays
// when in strtotime for some reason. It displays accurately to what is in the mySQL DB
$upDate = date("Y-m-d H", strtotime($row2[post_date]));
// Some variables to make life easier for later if statements if I ever get that far. Displays accurately.
$upDatehour = date("H", strtotime($row2[post_date]));
// trying simple subtraction
$hour = $currentDateHour - upDatehour;
// this is where the result is incorrect, what is wrong here? Any method I've tried gives me the same result, with or without strotime.. it's gotta be something simple, always is!
print strtotime($hour);
You can drastically simplify your code. I'd recommend refactoring it to use DateTime and specifically DateTime::diff().
$now = new DateTime();
$post = new DateTime($row2['post_date']);
$interval = $now->diff($post);
echo "Updated " . $interval->h . " hours ago";
Working example: http://3v4l.org/23AL6
Note that this will only show up to 24 hours difference. If you want to show all hours even for a difference of more than 24 hours, you'll need to figure in the days. Something like this:
$hours = $interval->h + ($interval->format("%a") * 24);
echo "Updated $hours hours ago";
Working example: http://3v4l.org/ilItU
If you are just trying to get the number of hours between two arbitrary times, the easiest way would be to get the difference in seconds of the two times, and then divide by 3600 to determine the number of hours between the two dates.
Here is a basic example:
<?php
$row2['post_date'] = '2013-12-02 07:45:38'; // date from database
$now = time(); // get current timestamp in seconds
$upDate = strtotime($row2['post_date']); // convert date string to timestamp
$diff = $now - $upDate; // subtract difference between the two times
$hours = floor($diff / 3600); // get the number of hours passed between the 2 times
echo $hours; // display result
Also, Wordpress has a built in function that may end up doing what your ultimate goal is, see wordpress function human_time_diff().
Example:
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
Result:
2 days ago.
Example how to get difference between dates in hours:
$diff = date_diff(date_create(), date_create($row2['post_date']));
$hours = $diff->days * 24 + $diff->h;
If you wish to format output number with leading zeros, you can use sprintf() or str_pad() function. Example of sprintf() use for HH:mm format:
echo sprintf('%02d:%02d', $hours, $diff->i);
demo

Subtract php format date and set the difference value in a textbox [duplicate]

How can I compute time difference in PHP?
example: 2:00 and 3:30.
I want to convert the time to seconds then subtract them then convert it back to hours and minutes to know the difference. Is there an easier way to get the difference?
Look at the PHP DateTime object.
$dateA = new DateTime('2:00');
$dateB = new DateTime('3:00');
$difference = $dateA->diff($dateB);
(assuming you have >= PHP 5.3)
You can also do it the procedural way...
$dateA = strtotime('2:00');
$dateB = strtotime('3:00');
$difference = $dateB - $dateA;
See it on CodePad.org.
You can get the hour offset like so...
$hours = $difference / 3600;
If you are dealing with times that fall between a 24 hour period (0:00 - 23:59), you could also do...
$hours = (int) date('g', $difference);
Though that is probably too inflexible to be worth implementing.
Check this link ...
http://www.onlineconversion.com/days_between_advanced.htm
I used this to calculate the difference between server time and the users local time. Grab the hour difference and drop that in a form when the user is registering. I then use it to update the time on the site for the user when they do stuff online.
Once I got it working, I switched this line ...
if (form.date1.value == "")
form.date1.value = s;
to ...
form.date1.value = "<?PHP echo date("m/d/Y H:i:s", time()) ?>";
Now I can compare the user time and the server time! You can grab the seconds and mins as well.

Calculating the elapsed time [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Determining elapsed time
I want to know how I can calculate the elapsed time beween 2 dates I have in a DB.
They are saved as 'EntryDate' and 'ExitDate' in date/time as DD-MM-YYYY 00:00:00 and I want to calculate the time between two for instance:
01.01.2012 12.00.00 and 01.01.2012 13.30.00 which would be 1hr 30 mins.
Thanks. Sam.
You probably want to use timediff, when working inside mysql.
mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
-> '2000:01:01 00:00:00.000001');
-> '-00:00:00.000001'
For the implementation in PHP you need to use date_diff function.
$d_start = new DateTime($EntryDate);
$d_end = new DateTime($ExitDate);
$diff = $d_start->diff($d_end);
// return all data
$this->year = $diff->format('%y');
$this->month = $diff->format('%m');
$this->day = $diff->format('%d');
$this->hour = $diff->format('%h');
$this->min = $diff->format('%i');
$this->sec = $diff->format('%s');
Use this forum thread: http://forums.devshed.com/php-development-5/calculate-remaining-day-minute-seconds-between-two-dates-538979.html
Next time google a little before posting. We will help you but you shouldn't abuse...
Because the dates are formatted like this, you can use strtotime to get the timestamps for each date. Then you just subtract the exit from the entry. This leaves you with the difference in seconds. Now you can play around with this.
you can use
eg:
select TIMEDIFF ('2006-10-31 11:50:31' , '2006-10-31 11:50:01')
SELECT TIMESTAMPDIFF(MINUTE,'2012-01-01 12:00:00','2012-01-01 13:30:00')
This will return the difference in minutes. You can replace Minute with any time unit (like 'HOUR', 'SECOND', etc.)

Figuring out the difference in days between events

I have a MySQL DB with StartDates in the format of yyyy-mm-dd and starttimes in the format of HH:MM using a 24 hour clock. What would be the easiest way to compare the difference of two days in PHP? Would using a datetime object could I set it to be with the information given and just give it to zeros for the seconds? I need to get the amount of time between both dates down to the minute. I was putting the startdate (Just the day since its always within the same month for my application) and time together concatenated together and then pulling out what I need like below, but I haven't been able to get it straight yet. Thanks for the look!
$tempvar1 = $times[$i][$j];
$tempvar2 = $times[$i][$j+1];
$day1 = $tempvar1[0].$tempvar1[1];
$day2 = $tempvar2[0].$tempvar2[1];
$hours1 = $tempvar1[2].$tempvar1[3];
$hours2 = $tempvar2[2].$tempvar2[3];
$minutes1 = $tempvar1[5].$tempvar1[6];
$minutes2 = $tempvar2[5].$tempvar2[6];
$numdays = ($day2-$day1) - 1;
$time1 = ($hours1*60)+$minutes1;
$time2 = ($hours2*60)+$minutes2;
MySQL has plenty of date/time functions:
SELECT TIMEDIFF(endtime, starttime), DATEDIFF(endtime, starttime)
FROM ...
doc links for timediff and datediff
That'll you get strings in the format of 'hh:mm:ss.ssss' for timediff, and a straight-up integer representing the days between the two dates, respectively.

Categories