tickets between two dates and two times [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have another question.
I get stuck by showing the tickets between two dates and two times.
I have following table fields:
Start date || ticketSDate, Start time || ticketSTime
End date || ticketEDate, End time || ticketETime
I want to show all the tickets where the End Date and End Time are bigger or equal as now
AND
Where the Start Date is less or equal as now
Can anyone help me out here?
Thanks, Benny
<?php
$result = mysqli_query($con,"SELECT * FROM ticket WHERE ticketEDate >= NOW() AND ticketETime > NOW() AND ticketSDate <= NOW() ORDER by ticketSDate ASC");
while($row = mysqli_fetch_array($result, MYSQL_BOTH)) {
echo '<tr>
<td><h8>'. $row['ticketName'] .'</h8></td><br>
<td><h4><strong>Begin:</strong> ',date("d.m.Y", strtotime ($row['ticketSDate'])),' ', date('H:i', strtotime ($row['ticketSTime'])).' hr.<h4></td>
<td><h4><strong>Einde:</strong> ',date("d.m.Y", strtotime ($row['ticketEDate'])),' ', date('H:i', strtotime ($row['ticketETime'])).' hr.<h4></td>
<td><h4>'. $row['ticketDescription'] .'</h4></td>
<td><h4>'. $row['ticketID'] .' <input name="book" type="button" value="Koop een ticket" /></h4></td>
<br><br>
</tr>';
}
?>

That's a flawed concept of thinking about time. You're saying
I want to show all the tickets where the End Date and End Time are bigger
So that means on a date which is after today but time is still not what it is right now, return false which is not reasonable at all.
So you have to use some grouped conditions. For example, End datetime is greater than now.
Now if the end date is greater than today, forget about time its meaningless.
WHERE ticketEDate > NOW() OR (ticketEDate = NOW() AND ticketETime > NOW())
Same concept for the start.
A very simple and better solution would be to store the date time as one field and then your query is a very simple one.

Related

Why does print strtotime('2020-11-02') - strtotime('2020-11-01'); return 90,000 seconds? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
print strtotime('2020-01-02') - strtotime('2020-01-01'); returns 86,400
print strtotime('2020-02-02') - strtotime('2020-02-01'); returns 86,400
etc.
print strtotime('2020-11-02') - strtotime('2020-11-01'); return 90,000 seconds
My apologies if this has been asked before. I checked and didn't find anything.
My program is calculating the days between two dates, counting the first date as one.
Here is a sample I'm working with:
<?php
function dateDifference($d1, $d2)
{
// calulating the difference in days
// get seconds between dates
$diff = strtotime($d2) - strtotime($d1);
// 1 day = 24 hours
// 24 * 60 * 60 = 86400 seconds
// '2020-11-02' and '2020-11-01' returns three days.
return ceil(abs($diff / 86400))+1;// Start day is day one.
}
$d1 = '2020-11-01';
$d2 = '2020-11-02';
$trip_days = dateDifference($d1,$d2);
// returns three days, not two.
echo 'Total Days between 11/01/2020 and 11/02/2020: ' . $trip_days . "\n";
$d1 = '2020-10-01';
$d2 = '2020-10-02';
$trip_days = dateDifference($d1,$d2);
echo 'Total Days between 10/01/2020 and 10/02/2020: ' . $trip_days . "\n";
?>
This year, daylight saving's ended on Sunday November 1st.
We got an extra hour which is 3,600 seconds,
86,400 + 3,600 = 90,000
This seems like a daylight savings time issue.
Some places, like Canada and some US states, changed from daylight savings time to standard/winter time on Sunday 1st of November 2020, making the clock jump from 02:00 back to 01:00. Therefore, you got an extra hour that night.
Source: https://www.timeanddate.com/time/dst/events.html

PHP calculate date interval from 0/0/0 00:00:00 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I need to calculate a duration and make it human readable.
e.g. I have a string: "2 days 4 hours". I need my code to calculate the duration of that string as a timestamp and to make it human readable.
My problem is that if I add the timestamp to 0, it starts from 1/1/1970, but I need it to just calculate the duration.
I tried to subtract 1/1/1970 01:01:00 to 0 but when calculating it returns a no-sense result.
If I understand you correctly, you would like to transform something like:
$dateString = "2 days 4 hours";
to timestamp?
If that is the case, and you know there will always be days/hours you could do something like:
$dateString = "2 days 4 hours";
preg_match_all('!\d+!', $dateString , $numbersOnly);
$partialTimeStamp= (($numbersOnly[0][0]*24)+$numbersOnly[0][1])*3600;
print_r($partialTimeStamp); // will print 187200
With this you will have a numbers of seconds. If for example you also have minutes in your string you would do:
$dateString = "2 days 4 hours 15 minutes";
preg_match_all('!\d+!', $dateString , $numbersOnly);
$partialTimeStamp= (($numbersOnly[0][0]*24)+$numbersOnly[0][1])*3600 + $numbersOnly[0][2]*60;
print_r($partialTimeStamp); // will print 188100
And with this partial timestamp, you can than calculate next timestamp like:
$nextTimeStamp = time () + $partialTimeStamp;
print_r($nextTimeStamp);
EDIT: Missed the "back to human readable" part..
For this, you would do something like:
$days = floor($partialTimeStamp/ 86400);
$partialTimeStamp = $partialTimeStamp - $days*86400;
$hours = floor($partialTimeStamp/ 3600);
$min = floor($partialTimeStamp/ 60 % 60);
And than just this:
print_r($days." days ".$hours." hours ".$min. " minutes");// will output 2 days 4 hours 15 minutes
I hope this helps.
BR

Compare difference between two dates PHP

I am trying to compare to date to figure out how much time is between them, which I know how to do, date_diff(), but I want to then compare the time between the dates and if it is greater than 7 days do something and if not do something else. I think it sounds easy and I know there are probably fairly simple solutions to do so but I am just not a fan of dates and comparisons. Here is a snippet of what I got so far as it is just one case of a switch statement so the rest are basically identical.
$array = array();
$today = date("Y-m-d"); // get today's date
foreach($arrayOfObjs as $obj){
if ($obj->get("renewalDate") >= $today){
array_push($array, $obj->get("renewalDate"));
}else{
switch($obj->get("recurrencePeriod")){
case 1:
/*
* All cases follow same structure
* Build the date in format Y-m-d from renewalDate out of the obj.
* Loop through the date while it's less than today.
* After date is greater than today return date add to array
*/
$date = DateTime::createFromFormat("Y-m-d", $obj->get('renewalDate'));
while($date <= $today){
$date->add(new DateInterval('P7D'));
}
$diff = date_diff($today, $date);
if($diff->format('%a') <= 7){
$obj->renewalDate($date);
array_push($array, $obj);
}
break;
Basically, my database stores dates and those dates could be passed but it could be a reoccurring event. To calculate the next time that event would happen I check if the data in the database is before today's date and if it is then I continue to add the incremental amount (in this case 7 for a weekly reoccurring event) and compare the dates again. After the date that is incremented passes today's date I want to find out if it is within 7 days and if so add it to an array to get returned. I know... since I'm adding 7and it's within 7 days the first reoccurring event will always be within 7 days but that is not the case for monthly events or anything greater.
All cases are broken so I only included this one for simplicity. I can get date_Diff to return something like 7 or 12 or whatever the number may be but how can I check if that number is within the 7 days I want?
Thanks, I will include more information if needed to clarify any misunderstandings.
I'm not entirely sure what you are trying to do, but how about the following if you are just projecting dates forward and backwards and want to know if they are 7 days or more either way:
$today = date("Y-m-d");
$todaytime = strtotime($today);
$testdate = "2017-06-31";
$testtime = strtotime($testdate);
$back7days = strtotime("-7 days",$todaytime);
if($testtime < $back7days)
echo "X"; // do somthing if testdate was more than 7 days ago
$fwd7days = strtotime("+7 days", $todaytime);
if($testtime > $fwd7days)
echo "Y"; // do somthing if testdate is more than 7 days in future
Just make sure that you use less-than or less-than-and-equals comparators etc to handle the boundary conditions you need.

Comparing dates (older, newer, or the same) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Date 1: dd/mm/yy
Date 2: dd/mm/yy
Date 3: dd/mm/yy
Say I have:
$date1 = 04/07/2014;
$date2 = 04/06/2014;
$date3 = 04/07/2014;
What would be the most efficient method to determine if date 2 is between or equal to date 1 and date 3 using PHP? In other words, what is the best way to check if the dates are the same, newer, or older than another date
The best way is to convert date into timestamp, then it will be easy to compare.
Try to use
$time1 = strtotime("dd/mm/yy");
$time2 = strtotime("dd/mm/yy");
Well, this really depends on what you are using this for, what I personally did (when checking dates in a MySQL database for a scheduling website I made) I used the following:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
You could do the same, input your date as Ynj (See PHP documentation on date here)
and then compare them. This may help put this whole idea in context; this is the code I wrote for my website:
$publishDate = $row['year'] . $row['month'] . $row['day'];
$now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
if($now > $publishDate)
{
//This is saying that right now, is greater than when this was added
$msg = "This date is smaller than the time now, which means it is in the past";
}
if ($now < $publishDate)
{
echo "This date is greater than the time now, which means it is in the future.";
}
if ($now == $publishDate)
{
echo "Date is the same";
}
If you add more context to your question I am sure I can help allot more! Good luck

Calculate age from given datetime stamp - PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a variable $dob which returns a date, in the following format: 1970-02-01 00:00:00
How can I use php to calculate the age of the person?
Your question is explained in detail in the PHP documentation, it bascially goes like this:
// create a datetime object for a given birthday
$birthday = new DateTime("2012-12-12");
// substract your timestamp from it
$diff = $birthday->diff($dob);
// output the difference in years
echo $diff->format('%Y');
Try this:
<?php
$age = '1970-02-01 00:00:00';
echo (int)((time()-strtotime($age))/31536000);
Output: 43, in years.
$dob = '1970-02-01 00:00:00';
$date = new DateTime($dob);
$diff = $date->diff(new DateTime);
echo $diff->format('%R%a days');
Basically stolen from here: http://uk1.php.net/manual/en/datetime.diff.php
Formatting options: http://uk1.php.net/manual/en/dateinterval.format.php
One-liner:
echo date_create('1970-02-01')->diff(date_create('today'))->y;
Demo.
i would try this
$user_dob = explode('-',$dob);
$current_year= date('Y');
$presons_age = $current_year - $user_dob[0];
This is an untested code but i feel u should get the logic.
strtotime() will convert your date into a timestamp, then subject that from time() and the result is the age in seconds.
$age_in_seconds = time() - strtotime('1970-02-01');
To display the age in years (+- 1 day), then divide by the number of seconds in a year:
echo "Age in whole years is " . floor($age_in_seconds / 60 / 60 / 24 / 365.25);

Categories