I have a date fields in database that have the day of the entry. The fields are starttime and stoptime, which define their schedule
For today of posting the start time is 2018-12-17 00:00:00 and the stop time is 2018-12-17 11:59:59
Example start times:
2018-12-17 15:30:00,
2018-12-24 06:00:00,
2018-12-17 10:00:00,
2018-12-17 09:00:00
and stop times (in the same order)
2018-12-17 16:00:00,
2018-12-24 07:00:00,
2018-12-17 12:30:00,
2018-12-17 10:30:00
it will show only 2018-12-17 09:00:00/2018-12-17 10:30:00
or everything else but the 1 above if i switch up the operation signs in if statement.
$starttime = date("Y-m-d H:i:s", strtotime('last monday', strtotime('next sunday')));
$stoptime = strtotime(date("Y-m-d H:i:s", strtotime($starttime))) + 43199;
$stoptime = date("Y-m-d H:i:s", $stoptime);
$stmt2 = $db->prepare("SELECT * FROM schedule WHERE day = :day");
$stmt2->execute(array(':day' => "Monday"));
while($row2 = $stmt2->fetch()){
if($row2['starttime'] >= $starttime AND $row2['stoptime'] <= $stoptime){
//data gets displayed here.
}
}
It looks like you have a misinterpreted date/time. From what you are supplying of 43199, this is 1 second LESS then 12HOURS, not a 24 hour day. So what you are getting are those entries that are before 12 mid-day time instead of 12 in the morning. On a 24-hour clock you would be at 86,400 for 24 hours... or 86,399 for 1 second less.
Additionally, you could update your query to just care about the start time as anytime within the starting date in question. If the ending time happens to roll-over to the next date, I am sure you want that in the result set too.
SELECT *
FROM schedule
WHERE startTime >= '2018-12-17'
and starttime < date_add( '2018-12-17', interval 7 days)
Now, I am doing LESS than 7 days in the above sample. So lets look at it.. The beginning date/time of '2017-12-17' is IMPLIED at hour:min 00:00 at midnight. By doing LESS than 7 days, it covers the entire week up to 11:59:59 PM (or 24hour clock 23:59:59) on the following Sunday. So it will be all LESS then Monday of the following week. Then you don't have to worry about formatting your specific strings in the query and then testing. Just get the results and go.
Related
I have a unix timestamp and a duration, such as a year. I'd like to find out how to determine how much time is remaining from the duration based off the start unix timestamp. I want it in monthes.
I imagine its something like:
duration convert to seconds.
minus start to today to seconds (elapsed)
minus elapsed from duration
convert remaining to remaining in seconds
change remaining to monthes and determine remainder to days
I'm sure thouhg theres a php solution which is more accurate and precise than my design i think.
UPDATE
I have a unix timestamp of 1564113711, which is July 26, 2019. Today is August 7,2019. I need to know how much time remains in monthes and days from July 26,2020 (one year from the start) to today, August 7, 2019, where the elapsed time is July 26,2019 to August 7,2019
The DateTime object and DateTime::diff have everything you need.
$timeStamp = 1564113711; //26. Juli 2019
$duration = "1 Year";
$endDate = date_create("#".$timeStamp)->modify($duration);
$diff = date_create("today UTC")->diff($endDate);
//output
if($diff->invert) {
echo "your time's up";
}
else {
echo $diff->y * 12 + $diff->m." Month and ".$diff->d. " Days";
//e.g. 10 Month and 24 Days
}
I'm making an auction house where the auction ends after 1 month of the date it started.
Here's what i got so far:
$added_on = date('Y-m-d H:i:s');
$ends_on = new DateTime('NOW');
$ends_on->add(new DateInterval('P1M')); // 1 month
$ends_on->format('U');
$stringdate = $ends_on->format('Y-m-d H:i:s');
I need the auction to end 1 month after but on a week day, if possible, between 9h-18h. Any idea how to achieve this result?
You need to check the day number of the new $ends_on date i.e. $d = $ends_on->format("w"); If 0 (Sunday) add 1 more day. If 6 (Saturday) add 2 more days. The times will need a bit more as when do you decide the cut off time. If a entry is made after 1800 does it still expire on the same day or the next day?
There are a lot of SO questions showing you how to check if a date-time is within a range, but there is not much to help when you want to do recurrently and the date range spans more than one day.
As an example, how do you check if its between 22:00 on a Tuesday and 07:30 on the following Thrusday? And how do you do that week in week out?
The constructor for PHP's DataTime class accepts the relative argument this xxxday, which can be used to give you the next 'xxxday' relative to the current time.
So for example, new DateTime('next friday') will give you a datetime object representing 00:00:00 on the next Friday relative to the current day. If the current day is Friday, then that is the date which will be returned.
So if you define
$end = new DateTime(next thursday 07:30);
then $end is a DateTime object representing 22:00 on the next Tuesday from the current time, or 20:00 for today if today is a Tuesday. Then you can take the interval off the end to get the start with
$start = clone $end;
$start = $end->sub(new DateInterval('P1DT9H30M'));
which gives you a DateTime object 1day, 9hours and 30 minutes prior to 07:30 on the next Thursday from the current time, or 22:00 on the Tuesday before the next Thursday from the current time.
Then you can get the current date time and see if its between the start and end date times:
$now = new DateTime();
if( $now >= $start && $now <= $end ) {
//In interval
}
I just ran into a strange bug that has occurs in PHP running in Pacific Time (and probably others). I had code to get the first day of a week (sunday) given an arbitrary date (in UNIX timestamp) in that week:
$day = date('w', $date);
$start_of_week = date('Y-m-d', $date - ($day * 60*60*24));
echo $start_of_week;
Prints 2014-03-08
This works for every single date that I've tried, except for those in the week of March 9th, 2014, which happens to be the week of daylight savings time in the US. For those, $start_of_week is '2014-03-08', which is a Saturday.
When I run this code with the timezone set to GMT, I get the correct output ('2014-03-09').
Additionally, when I change the code to the following in PST, I get the correct output:
$day = date('w', $date);
$start_of_week = date('Y-m-d', strtotime("-$day day", $date));
echo $start_of_week;
Prints 2014-03-09
So...WTF? Why is there a difference between strtotime("-1 day", $date) and $date - 60*60*24? Seems like it's jumping between different timezones.
codepad example
When you use -1 day, it uses the time-of-day from $date, and just changes the date. When you use - 60 * 60 * 24 it rolls the clock back 24 hours. On the day that DST changes, there are only 23 hours in the day, so it goes an hour too far.
– Barmar Mar 19 '14 at 22:11
I want same day of month for each of the months that fall between start date and end date. Its just that if the month of the day is not valid for a particular month, you want last day of that month. is there any script?. What is have done is.
$startdate='2010-01-30';
$enddate='2011-01-30';
while ($startdate <= $enddate)
{
echo date('Y-m-d', $startdate ) . "\n";
$startdate = strtotime('+1 month', $startdate);/// for case of feb 28 days last date it should disply as it skips it
}
Expected out put:
For input $startdate='2012-01-30'; $enddate='2013-12-30' Result should be like this ==>
_2012-12-30_
2013-01-30
**2013-02-28**
2013-03-30
2013-04-30
2013-05-30
2013-06-30
2013-07-30
2013-08-30
2013-09-30
2013-10-30
2013-11-30
_2013-12-30_
Loop using for and adding $i months to the start date instead of constantly adding one month to the running value. This way it won't jump to 28th (or 29th) day because of february starting from march.