This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to figure out the difference in time by seconds. I keep getting 0 as a results and wants to know if someone could assist me. The times will be pulled from a database.
Here is an example of what I want to be able to do.
$TimeStart = "04:30:45 am";
$TimeEnd = "04:31:46 am";
$Difference = $TimeEnd - $TimeStart . "seconds";
I know this is not a very illustrated example and I know it is probably not that simple.
Here is the code that is currently on my site now:
$TimeStart = strtotime($VisitsRow['TimeStart']);
$TimeEnd = strtotime($VisitsRow['TimeEnd']);
$TimeDifference = ($TimeEnd - $TimeStart);
if ($VisitsRow['TimeEnd'] == "") {
$Timeto = "Incomplete";
} else {
if ($TimeDifference <= 60) {
$Timeto = "< a minute";
} else if ($TimeDifference <= 0) {
$Timeto = "< a second";
} else {
$Timeto = round($TimeDifference, 2)." seconds";
}
if ($TimeDifference >= 60) {
$MinutesDiff = $TimeDifference / 60;
$Timeto = round($MinutesDiff, 2)." minutes";
}
}
I have simply tried the example of what I want above and am getting a negative number. This is why I am needing assistance.
Thank you everyone for helping me solve this. My problem was that it was gathering the times in 24 hr format and not the standard. Once I solved this everything works perfectly.
It's pretty easy with strtotime:
$TimeStart = strtotime("04:30:45");
$TimeEnd = strtotime("04:31:46");
$Difference = ($TimeEnd - $TimeStart) . " seconds";
But, if you need it to be more dynamic, through SQL, you can subtract the two date fields with this:
SELECT TIMESTAMPDIFF(MINUTE,TimeEnd,TimeStart) AS TimeDifference FROM myTable
(Note you can change MINUTE to another time metric if you want)
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to return all days between a specific range.
My idea was to convert the start and end date to unix timestamp and loop through them adding 86400 (seconds of a day):
<?php
$start = strtotime('2013-01-01');
$end = strtotime('2013-02-01');
for($i=$start; $i<=$end; $i+86400)
{
echo date("l, d.m.y", $i) . "\n";
}
?>
Unfortunately, I only get the same day:
Tuesday, 01.01.13
Tuesday, 01.01.13
Tuesday, 01.01.13
...
The best practise is to use the DatePeriod class.
$start = new DateTime('2013-01-01');
$end = new DateTime('2013-02-01');
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $date) {
echo $date->format("l, d.m.y\n");
}
This is wrong:
for($i=$start; $i<=$end; $i+86400)
should be
for($i=$start; $i<=$end; $i+=86400)
Notice the += insetad of + of your original code. In your code, you didnt assign new value to variable, just perform mathematical formula without result
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
$start_date = "2012-01-05";
$end_date = "2012-02-10";
How to get month and after getting month date should like in above example
$end_date should "2012-02-05"; leave above 5 days. don't count a above day.
How can i do this in php?
I wan't to like
$month = 1;
$end_date = "2012-02-05";
Second Example
$start_date = "2012-06-19";
$end_date = "2012-09-25";
then should
$month = 3;
$end_date = "2012-09-19";
Leave days between "2012-09-19" to "2012-09-25".
Thanks
$start = '2012-01-23';
$months = 12;
$date=new DateTime($start);
$date->modify('+'.$months.' month');
echo $date->format('Y-m-d');
You can use strtotime() to just add a number of months to your original date...
$start = '2012-01-23';
$months = 3;
$timeExpr = sprintf('+%d months', $months);
$end = date($start, strtotime($timeExpr));
echo $end; //will output '2012-04-23'
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
The user enters a time to hire a bus and the length of hours. I then query this information from mysql database. I find the end time of the hire and figure out how many hours after midnight the hire is for, if any. I'm pretty sure I had this working but now it just isn't working and I cannot see why it won't. Finding the end time is right but my if statement doesn't seem to work. For example, when the end time is "02:00:00" so 2am, it follows the path of the if statement so that the time after midnight is 3 hours, when it should be 2 hours. Any help as to why? Thanks.
($hireid = latest hireid in database)
//GET LATEST TIME AND LENGTH OF HIRE
$result = mysql_query("SELECT time, length FROM hire WHERE hireid='$hireid'") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_assoc($result)){
$time = $row['time'];
$length = $row['length'];
}
//FIND AMOUNT OF HOURS BEFORE AND AFTER MIDNIGHT
echo "Time: " . $time . "</br>";
echo "Length: " . $length . "</br>";
echo "Hours: " . $hours . "</br>";
echo "End time: " . $endtime = date('H:i:s', strtotime($time . '+ ' . $length . 'hours')) . "</br>";
$hoursafter = 0;
if ($endtime = "01:00:00") {
$hoursafter = 1;
}
if ($endtime = "02:00:00") {
$hoursafter = 2;
}
if ($endtime = "03:00:00") {
$hoursafter = 3;
}
echo "Hours after midnight: " . $hoursafter . "</br>";
you should do:
if ($endtime == '02:00:00'){
$hoursafter = 2;
}
You just mis one = in your if statement. If you change this by all your statements in the script and it will work :)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to create a yearly automated schedule for a publication. Every issue has 3 dates (Wednesday, Friday & Monday), that occur every two weeks. And in some cases when the day falls on a holiday it should automatically adjust and goto the following day.
I'm trying to do this all right now in PHP, I've got most of it working but after issue 7 the script goes wrong and seems to skip a week. Can anyone help or suggest a way of achieving this?
Here is pseudo-code of my logic;
For 12 months
Get the amount of days in a given month
For days in a month
If Wednesday
assign date to a var
Else if Friday
assign date to a var
print wednesday var
print friday var
print monday var
increment counter in order to skip ahead to the next week
Else if Monday
assign date to a var
Thanks
I think this should point you in the right direction (untested, but should do what you want).
$iYear = date('Y');
for($i = 1; $i <= 12; $i++) {
$iNumDaysInMonth = date('t', mktime(0,0,0,$i,1,$iYear);
for($j = 1; $j <= $iNumDaysInMonth; $j++) {
$iDayNum = date('N', mktime(0,0,0,$i,$j,$iYear));
if($iDayNum == 3) {
// wednesday
} elseif($iDayNum == 5) {
// friday
} elseif($iDayNum == 1) {
// monday
}
}
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm getting the weekdays from a given week number and year using the php below:
$week_number = 42;
$year = 2014;
for($day = 1; $day<=7; $day++)
{
echo date('m/d/Y',strtotime($year."W". $week_number.$day));
}
The Output look likes this:
10/13/2014
10/14/2014
10/15/2014
10/16/2014
10/17/2014
10/18/2014
10/19/2014
How can I make it look just like this:
Oct 13 - Oct 19.
Thank you.
<?php
$week_number = 42;
$year = 2014;
echo date('M d',strtotime($year."W".$week_number . 1)) . " - " . date('M d',strtotime($year."W".$week_number . 7)).".";
?>
$week_number = 42;
$year = 2014;
$week_number = ($week_number < 10) ? '0'.$week_number : $week_number;
echo date('M d',strtotime($year.'W'.$week_number.'1')).' - '.date('M d',strtotime($year.'W'.$week_number.'7')).'.';
// remember that $week_number must be prefixed with 0 if week number is lower than 10
you can use strftime
here is the link http://php.net/manual/en/function.strftime.php
Then use
date("F j, D");for your date function