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 :)
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.
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)
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 have dates array like this
$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');
Now if I have a current_date say
$current_date = '2010-10-11';
how would I find whats the closest PAST date to this. Which in this case would be 2012-10-07
thanks
Try like this
$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');
echo "<pre>";
print_r($dates);
$dateArray=array();
foreach($dates as $row)
$dateArray[] = date('Y-m-d', strtotime($row));
$current_date = date('Y-m-d', strtotime('2012-10-11'));
array_push($dateArray,$current_date);
sort($dateArray);
echo "<br />";
print_r($dateArray);
echo "</pre>";
echo "<br />";
$cd_index= array_search($current_date, $dateArray);
if($cd_index>0)
{
$pastdate=$dateArray[$cd_index-1];
echo "Pastarray-".$pastdate;
}
else
echo "No Past Date";
For Future Date:
if($cd_index<(count($dateArray)-1))
{
$pastdate=$dateArray[$cd_index+1];
echo "Pastarray-".$pastdate;
}
else
echo "No Future Date";
$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012,10,20');
$current_date = '2010-10-11';
if(!array_search($current_date, $dates))
array_push($dates, $current_date);
usort($dates, function($a1, $a2) {
return strtotime($a1) - strtotime($a2);
});
$my_current_date_index = array_search($current_date, $dates);
$my_previous_date = $my_current_date_index == 0 ? 'There is no previous date' : $dates[$my_current_date_index - 1];
this can also be used too..
<?php
$dates = array('2012-10-07','2012-10-02','2012-10-03', '2012,10,20');
$current_date = date('2010-10-11');
$closest='';
foreach($dates as $d)
{
if( date('y-m-d',strtotime($d))<$current_date && $d>$closest)
{
$closest=$d;
}
}
echo $closest;
My solution:
// your test data
$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');
$current_date = '2012-10-11';
// array to help analyzing the file
$myResolver = array();
// processing the array and calculating the unixtimestamp-differences
array_walk($dates,'calc_ts', array(&$myResolver, $current_date));
// sorting the array
asort($myResolver);
// fetching the first (aka smallest) value
$closest = key($myResolver);
var_dump($closest);
// calculating the unixtimestam-differences
function calc_ts($item, $index, $d) {
$d[0][$item] = abs(strtotime($item) - strtotime($d[1]));
}
Works both with dates before and after your desired date.
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