Difference between two dates in MySQL and display in php - php

I have 2 dates a Checkin Date and Check out Date on a booking system. i want to find out the amount of days from checkin to checkout. I can run a Select query in mysql and get my results. But i am trying to work out ho to do it easy in php.
I do not know how to display the query results in each row of the bookings
My sql query:
SELECT timestampdiff(DAY, bookeddate, bookeddateout) AS days
FROM users
this works fine and creates temp column called days.
I am newish to this and i am not sure what to do. sorry guys. Al i want is to display the days a person is staying in a column on the users table.

Yo can use this function i write call as dataDiff($firstDay,$lastDay)
function dataDiff($date1, $date2) {
$dateDiff = 0;
$nextday = $date1;
while($nextday <= $date2) {
$nextday = date("Y-m-d", strtotime("+1 day", strtotime($nextday)));
$dateDiff++;
}
return $dateDiff;
}

Related

Query CodeIgniter NOW() Sysdate() or .?

i have a problem with query SQL.
I need do a query that it result my order to last week ( 7days to now) I use this code
$this->db->where("account_orders.data > now()-7",NULL);
date in my db is -> 2019-04-05
Why don't it work ?
I dont know why are using this now()-7, you can use range here with where clause if you want orders between 7 days to now, as:
$dateLastWeek = date('Y-m-d', strtotime('-7 days')); // last week day
$dateCurrent = date('Y-m-d'); // current day
In CI, you can use range as like:
$this->db->where("account_orders.data >=", $dateLastWeek);
$this->db->where("account_orders.data <=", $dateCurrent);
If you just need last week orders then, you can use just $dateLastWeek date in your query as:
$this->db->where("account_orders.data", $dateLastWeek);
Note that, i am using Y-m-d date format due your input which you mentioned in your question 2019-04-05

php future date showing 1970-04-01 instead of 3 months after start date

I'm trying to take a date and add 3 months to it for use on an accounting system and we need to split the dates into quarters for our tax return
I have the following code
$e = ORM::for_table('sys_taxdate')->find_many();
This has been input into the database using a date type in the column and displys correctly as :
2016-07-02
I then want to add 3 months to this date so i can search the database for any invoices rasied between these 2 dates. The code i have tried is
$idate = $e;
$its = strtotime($idate);
$dd = date('Y-m-d', strtotime('+3 months', $its));
But $dd outputs the date 1970-04-01
I have looked over many posts and looks as though strtotime is not starting with the right date format, or at least thats what i think but have been trying for hours now and have hit a brick wall
I figured it out.
I ended up getting the exact result from the database first
$e = ORM::for_table('sys_taxdate')->find_one(2);
$date = $e->get('taxdate');
$idate = $date;
$its = strtotime($idate);
$dd = date('Y-m-d', strtotime('+3 months', $its));

trying to hide an event two days after it's passed

I'm trying to hide an event two days after it's passed in php (using a mysql query). There are two date options. Start date, and End date. I can't seem to figure out how to make the query work.
$query->select('*');
$query->from('#__events_items');
$query->where('date2 >= "'.$today.'"');
$query->where('date2 <= "'.$sixmths.'"');
$query->where('state = 1');
$query->order('date1 asc');
I've tried
$today = #date('Y-m-d');
$enddate = #date('Y-m-d',(strtotime(#date('Y-m-d')."+ 2 days")));
But obviously the end date won't be greater or equal to $enddate.. Any help would be greatly appreciated!
You can either modify today like so:
$today = date('Y-m-d', strtotime('-2 days'));
Or modify your query like so :
$query->select('*');
$query->from('#__events_items');
$query->where('date2 >= "'.$today.'" - INTERVAL 2 DAY');
$query->where('date2 <= "'.$sixmths.'"');
$query->where('state = 1');
$query->order('date1 asc');
I would say the second is probably better form since having $today represent two days ago would be nasty self documenting code.

Repeat insert query with a different date given by a frequency

I'm building a system where an user can register activities. However the activities registered can repeat over the course of the year.
In order to prevent having the need that the user has to fill in the form to create an activity multiple times for each different date, I had the idea to add a textbox and a dropdown to the form to allow the user to fill in a frequency. The user can fill in a number in the textbox (for example "2") and select a value from the dropdown (for example "week"). So from that selection the activity has to be added to the database for the next 2 weeks on the same day.
However I have no idea how to let PHP adjust the date and add exactly 7 days to the selected date and repeat the same insert query with the new date, for every week/month/year selected from the given frequency.
EDIT 1:
I've tried this so far:
while ($i> 0)
{
$query2 = $this->db->connection->prepare("INSERT INTO activity(Act_Startdate) values (?)");
$query2->bind_param("s", $Startdate);
$query2->execute();
$Dates = date('d-m-Y', strtotime($Startdate . '+ 1 days'));
$Startdate = date('d-m-Y', strtotime($Dates));
$i--;
}
The first date insertion works, but the second one results 0000-00-00.
Read more about :
Date Time in PHP.
Date Interval in PHP
$numberPostedByUser = $_POST['your_input_name_in_form'];
$currentDate = new \DateTime(); // Getting current date
$iterator = 1;
while ($iterator <= $numberPostedByUser) {
$currentDate->add(new \DateInterval('P1D')); // Adding interval of one day in current date
$startDate = $currentDate->format('Y-m-d H:i:s'); // converting that day in convenient format we required
$query2 = $this->db->connection->prepare("INSERT INTO activity(Act_Startdate) values (?)");
$query2->bind_param("s", $startDate);
$query2->execute();
$iterator++; // increasing iterator for next loop
}
Hope may this code will help you.

Date comparison working as long as time is 00:00:00

this is my events script that pulls out appointments for the next 7 days, it appears to work ok, but only under one condition........The dates and times are held in the mysql db in datetime format so 2013-12-23 08:30:00 . My script prints out each day and finds appointments for that day for customers that are dropping off or picking up things. The mysql looks through the db and matches the customers with the dropping off or picking up fields to the date being printed and adds them in the div below the date.
The problem I am having is that if the time is set to anything other than 00:00:00 it doesn't pickup that customer for that day. How do I get the comparison to ignore the time and only use the date ?.
// Date box container
echo '<div class="dateboxcontainer">';
// Loop through and create a date for the next 7 days
$days = new DatePeriod(new DateTime, new DateInterval('P1D'), 7);
foreach ($days as $day) {
echo '<div class="datebox">';
echo '<div class="topdate">';
echo strtoupper($day->format('D d')) . PHP_EOL;
echo '</div>';
// Get the names for each day
$theday = strtoupper($day->format('Y-m-d'));
$sqldate = <<<SQL
SELECT *
FROM `jobdetails`
WHERE datedroppingoff = '$theday' OR datepickingup = '$theday'
SQL;
if(!$resultdate = $db->query($sqldate)){
die('There was an error running the query [' . $db->error . ']');
}
while($rowdate = $resultdate->fetch_assoc()){
echo $rowdate['name'];
}
//
echo '</div>';
}
echo '</div>';
//
What you are doing right now is comparing date/time values to just date values. This comparison would fail if the time part is anything other than midnight.
You can fix the comparison by using the DATE() MySql function to compare apples with apples:
WHERE DATE(datedroppingoff) = '$theday' OR DATE(datepickingup) = '$theday'
There are other ways to do the same, for example
WHERE DATEDIFF(datedroppingoff, '$theday') = 0 OR ...
If you had a $nextday value at hand you could also do
WHERE (datedroppingoff >= '$theday' AND datedroppingoff < '$nextday') OR ...
You are storing a specific time and day in mySQL, but only search for a date in your SQL query. As mySQL does not understand the difference between you wanting to search for a complete day or a specific point in time, mySQL assumes you are looking for the day at time 0:00:00.
You have a few options, you could search for a time period (pseudo code, check the borders yourself):
WHERE datedroppingoff > '$theday' AND datedroppingoff < '$theday'+1
another option is to store the date and time in separate db fields. That way you can keep your SQL queries simpler.
Good luck.

Categories