Editing the question.
I have SQL like this:
`table1`.`DateField` >= DATE_SUB(NOW(), INTERVAL {$days} DAY
Now 24 hours make a whole day. However, what if I want to do the query for the last 3 hours or so?
My table1.DateField is in the format of 2010-03-10 10:05:50.
Original post:
If I have this
1 hour
2 hours
3 hours
..
24 hours
How would I change it to days?
Thanks.
$hours = 80;
$hid = 24; // Hours in a day - could be 24, 8, etc
$days = round($hours/$hid);
if( $days < 0 )
{
echo "$hours hours";
}
else
{
echo "$days days";
}
This assumes you want the hours if it's less than 1 day. If not just remove the switch.
MySQL not only knows DAY as a unit for an interval but also HOUR, MINUTE, ....
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
$x = 32;
$sql = "SELECT
x,y,z
FROM
foo
WHERE
`table1`.`DateField` >= NOW() - INTERVAL $x HOUR
";
As simple as:
if you want to convert the total of those hour to day:
Just sum the total of hours and that total must be divided by 24
(1 + 2 + 3 + 5) / 24
If you want to convert all of those hours to days:
Just divide by 24 every hours in your list
(1/24) (2/24) (3/24) (5/24)
Related
I'm having problem when getting the exact number of days. Given I have date/time which consider hours in counting number of days below the code give me zero days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-07 11:10:00");
$days=$fisrstDate->diff($secondDate)->days;
another example is this it should give me 2 days but shows only 1 days my idea is when 24 hours exceed I want to add another 1 days so that it would give me an output of 2 days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-08 05:00:00");
$days=$fisrstDate->diff($secondDate)->days;
You can use strtotime to get the exact seconds between two time stamps and then convert it to days followed by ceil to make it work. Eg:
$fisrstDate = strtotime("2018-03-07 04:46:00");
$secondDate = strtotime("2018-03-07 11:10:00");
$days = abs(ceil((abs($fisrstDate - $secondDate)/ (60 * 60 * 24)) - (1 / 24)));
echo $days;
Isn't it just date2 - date1 + 1?
I want to display a number of hours in days and hours as a human readable string but I need that 1 day be equal to 7 hours, so a working day.
I found this solution, but based on 1 day equal to 24 hours :
function secondsToTime($seconds) {
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
source: Convert seconds into days, hours, minutes and seconds
DateTime won't help here. You need to count it on your own.
But this is simple math. Integer division for days and modulo for hours.
$hours = 12345;
$days = floor($hours/7);
$restHours = $hours%7;
echo "{$days} days, {$restHours} hours"; //1763 days, 4 hours
Custom made code to deal with this (probably on of 1000 ways on can implement this, I don't claim this to be the best, or good or anything like that).
function secondsToTime($seconds) {
$totalMinutes = intval($seconds / 60);
$totalHours = intval($totalMinutes / 60);
$totalDays = intval($totalHours / 7);
echo "$totalDays days and ".($totalHours%7)." hours ".($totalMinutes%60)." minutes and ".($seconds%60)." seconds";
}
e.g.
secondsToTime(8*60*60);
prints
1 days and 1 hours 0 minutes and 0 seconds
im trying to get difference of date/time from a field type datetime to "right now" using php and mysql as database
this code is working fine, returns the output beautifully ok as required
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%d days %h hours %i minutes');
that is ok so far, no issues as this function is for php 5.3 and i have it on server
my need is 4 small things actually
1) how to eliminate the need for days?
i want to have (25 hours 10 minutes) instead of (1 day 1 hours 10 minutes)
2) how i can make $elapsed be bold or colored if the value is more than 5 hours for example!? simple IF logic will not work as the output is not actually a predefined value...
3) if the days or hours are 0, then want to remove them!
- For example if showing (0 days 10 hours 40 mins) then no need to display the (0 days), should show (10 hours 40 mins) that is enough
- Another example: 0 days 0 hours 45 minutes then to show only "45 minutes" no need for days and hours!
4) if output less than 5 minutes in total (0days 0 hours 1-5mins), then wanna make it show like "a while ago" only no need for any days, hours or minutes... then after 6 minutes.. go like "6 mins"
shortly something like facebook!?
okay, what i searched tried is different combination of workarounds but never worked as you know this interval is for php 5.3 and still seem not widely used?
any hint for one or more parts of this long question is appreciated,
M, Derik
tried to answer all your questions. so read the comments because i didnt write numbers for the problems. sorry
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$minutes = round(abs($datetime1 - $datetime2) / 60,2); //to calculate total time in MINUTES
if($minutes < 5) // for awhile ago problem.
{
return "awhile ago";
}
elseif($minutes > 6 && < 60)
{
return $minutes." minutes ago"; //for 6 minutes and after.
}
elseif($minutes>60) // for the hours..
{
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
$string = $hours. " hours and" . $minutes . " minutes ago.";
if($hours>5) //for bolding characters after
{
return "<b>".$hours." hours and".$minutes." minutes ago.</b>"; //for bolding character
}
else
return $hours." hours and".$minutes." minutes ago.";
}
hope this helps.
I'm going to write a function to print a number of days left between two dates. I would like it to tell the months and days left. For example:
45 days = 1month, 15 days
65 days = 2months, 5 days
10 days = 10 days
So I tried:
<?
$days=50;
if($days>"31"){
$time=$days/30;
}
echo $time;//1.67 month
?>
According to the code above. I expect the result to be like:
1 month, 20 days
Could you guys please suggest.
Try:
$days = 50;
if ($days > 31){
$month = floor($days/30); // return lowest whole integer
$days = $days % 30; // calculate left days
}
echo $month . " => " . $days; // output `1 => 20`
Get the month number for both months and subtract. Add in calculation for year change
Get day of months for both dates. If date2 > date1, subtract and you have the number of days, else remove 1 from month count and sumteact date
I have a SQL Datestamp like this: 2012-02-20 21:14:54
How would I print out the relative date and time in PHP?
e.g.
Occured: a few seconds ago
Occured: 4 minutes ago
Occured: 4 hours ago
Occured: Monday Jan 8th, 2012
After the hours I just want to print out the actual date
Found this after two seconds of Google http://www.mdj.us/web-development/php-programming/another-variation-on-the-time-ago-php-function-use-mysqls-datetime-field-type
In general you chose a unit of time like seconds, test if the time-difference is smaller then the max-value for this unit (60s) and if so, print out "$timeDifference $unit". If not you divide the difference by the units max-value and start over with the next higher unit (minutes).
Example:
$timeDif = 60*60*5 + 45; // == 5 hours 45 seconds
// 60 seconds in a minute
if ($timeDif < 60) // false
return "$timeDif second(s) ago";
// convert seconds to minutes
$timeDif = floor($timeDif / 60); // == 300 = 5 * 60
// 60 minutes in an hour
if ($timeDif < 60) // false
return "$timeDif minute(s) ago";
// convert minutes to hours
$timeDif = floor($timeDif / 60); // == 5
// 24 hours in a day
if ($timeDif < 24)
return "$timeDif hour(s) ago";
// ...
Here's what a MySQL solution might look like:
SELECT date_field, IF (DATEDIFF( NOW(), date_field) < 1, IF ( TIMEDIFF( NOW(), date_field ) < '01:00:00', CONCAT(MINUTE(TIMEDIFF(NOW(), date_field)), ' minutes'), CONCAT(HOUR(TIMEDIFF(NOW(), date_field )), ' hours')), date_field) AS occurred
FROM table
That is no so hard to code. Moreover is quite fun make it. Here a sample, Just put hands to work.
Start converting SQL Datestamp to unixtime like this:
$unixtime = strtotime('2012-02-20 21:14:54');