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.
Related
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;
}
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.
I have a function that is accepting the date and time, and number of occurrences of an episode. I'm using a while loop to try and insert and episode every week on the same day and time. For example if the episode is monday at 7PM, i want to insert in for every monday at 7PM for the number of occurrences given.
Here's my code and while loop:
$sEpsAirDate = strtotime($aVars['air_date'].' '.$aVars['air_time'].$aVars['air_ampm']);
$i = 1;
while ($i <= $aVars['repeat_count']) {
$sEpsAirDate = // How can I alter this variable to change the date to every week?
db_res(
"INSERT INTO `hm_episodes_main` SET
`show_id` = '{$aVars['show_id']}',
`title` = '{$sEpsTitle}.{$i}',
`season` = '{$aVars['eps_season']}',
`uri` = '{$sUri}.{$i}',
`desc` = '{$sEpsDesc}',
`air_date` = '{$sEpsAirDate}'
");
$i++
}
How would I alter the $sEpsAirDate variable to be entered accurately on every day of the week on the given time?
Use mktime():
$next_ep_timestamp = mktime ($hour,$min,$sec, $first_ep_month, $first_ep_day + 7 * $weekcount, $first_ep_year);
"Init" this by setting the respective variables for the date, month and year of the first episode, then you can create new dates for following weeks by adding increments of 7 to the day-parameter in mktime (like shown above).
Then format for output to SQL like this:
$datetime_str = date("Y-m-d H:i:s", $next_ep_timestamp);
//gives a date-str like '2011-10-16 12:59:01'
The first idea that comes to my mind is just adding the seconds in a week to the sEpsAirDate with every iteration in the loop:
$sEpsAirDate += 604800;
If you needed to preserve the first air date you could copy it out into a separate variable and then do something like this (change the LCV $i to start at 0):
$sEpsAirDate = $sEpsFirstAirDate+(604800*$i);
But this method has the potential to create problems with Daylight Savings Time... so it might be safer to break the date into year, month and day variables and then recreate the $sEpsAirDate with every loop iteration by adding ($i*7) to day. ... So something like (again change the LCV $i to start at 0):
$sEpsAirDate = mktime($sEpsAirDateHour, $sEpsAirDateMinute, 0, $sEpsAirDateMonth, $sEpsAirDateDay+($i*7), $sEpsAirDateYear);
Please help a newbee. I have a table named event_calendar with fields named ec_start_date, ec_end_date and ec_event_name. The first two fields are date fields. It is a small table, with less than 100 entries. I want to list events with a start or end date in the current month, then I want to follow with a list of events with a start or end date in the next month. The two list will be headed by the month name. I would like the dates displayed in the list to be in the format dd/mm.
This is the code I've found to identify the months for the list headers.
$thismonth = mktime(0,0,0,date("n"));
$nextmonth = mktime(0,0,0,date("n")+1);
echo "<h2 class='caps'>";
echo date("n", $thismonth);
echo "</h2>";
echo "<h2 class='caps'>";
echo date("m", $nextmonth);
echo "</h2>";
This is the code I use to pull the entries for this month's (August) activities
$query = "SELECT ec_display,ec_event_name,ec_start_date,ec_end_date FROM event_calendar WHERE month(ec_start_date) = 8 OR month(ec_end_date) = 8 ORDER BY ec_start_date";
The problem is, if I replace the number 8 with the variable $thismonth, it fails.
Finally, how can I display only the dd/mm from ec_start_date and ec_end_date?
I greatly appreciate any guidance, but please be specific as I am very new to this! Thank you!
$thismonth contains a UNIX timestamp returned by mktime. The timestamp for hour 0, minute 0, second 0 of month 8 of this year is 1312862400. That's not 8.
Don't put that in your query, put date('n') (8) in it... or just let MySQL do it
$query = "SELECT ec_display,ec_event_name,ec_start_date,ec_end_date FROM event_calendar WHERE month(ec_start_date) = MONTH(CURRENT_DATE) OR month(ec_end_date) = MONTH(CURRENT_DATE) ORDER BY ec_start_date";
I am trying to figure out the best approach for designing a "7 day calendar" that will consist of an HTML table with the columns "Name, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday". The HTML table rows will be filled with data from my database, ie. Name will have a persons Name, Sunday will show what that person needs to do on Sunday, ie "Brush teeth", etc etc. It's very similar to event calendars, except what I am looking to accomplish doesn't require the hourly view, just a simple 7 day, Sunday to Saturday view.
My database currently consists of "Name, EventDetails, and EventDate".
My HTML table columns consists of the columns "Name - Sunday - Monday - Tuesday - ..."
My Logic: Each time the page loads, the script will query the database and see if there are any EventDate entries that equal one of the 7 days of the week currently being viewed. If an EventDate matches, it will list itself in a row that matches the corresponding HTML table column of that date. Clicking "Previous Week" or "Next Week" would change to another week and should restart the script, but this time it will be using a different list of days to check against.
Anyone care to share some examples of what they can come up with to accomplish this?
Here is what I came up with so far... The problem with it is that if there are more than one event under a person, it makes a new row for each event whereas I'm working on getting it to list each event in one row.
<table border='1'>
<tr>
<th>Name</th>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<?php
function getCurrentWeek()
{
$weekArray = array();
// set the current date
$date = date("m/d/Y"); //'03/08/2011';
$ts = strtotime( $date );
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 0;
if ($offset < 0) $offset = 6;
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i=0; $i<7; $i++, $ts+=86400){
$temp_date = date("Y-m-d", $ts); // Reformat the dates to match the database
array_push( $weekArray, $temp_date );
}
return($weekArray);
}
$currentWeek = getCurrentWeek();
// Loop through the data array
while($row = mysql_fetch_array($result)){
$eventDate= $row['EventDate'];
// Loop through the currentWeek array to match a date from the database from a date in the current week
foreach ($currentWeek as $weekDay){
// If there is a matching date...
if ($eventDate == $weekDay) {
echo "<tr><td>".$row['Name']."</td>";
foreach ($currentWeek as $weekDay2){
if ($eventDate == $weekDay2)
echo "<td>".$row['EventName']."</td>";
else
echo "<td></td>";
}
echo "</tr>";
}
}
}
?>
</table>
To select a week you could do something like
set #weekday:= dayofweek(#adate);
set #StartOfWeek:= date_sub(#adate,INTERVAL #weekday DAY);
set #EndOfWeel:= date_add(#adate,INTERVAL (7- #weekday) DAY);
Then to select the week I'd do
SELECT * FROM TableWithEvents
WHERE TableWithEvents.EventDate
BETWEEN date_sub(#adate,interval #weekday day)
AND date_add(#date,INTERVAL (7-#weekday) DAY);
Note that using
#adate - #weekday will not work, you must use date_sub/date_add with the silly interval syntax.
It does work rather nice when adding months, where it correctly adds the number of days in a month or with years where it knows about leap years (but I digress).
For the pagination you can use the above SELECT with limit start, end; like so:
SELECT * FROM sometable WHERE some_where_thingy LIMIT 0,20;
O and don't forget to add an index to the EventDate field.
And I would recommend adding an autoincrement primary key named id to the table with events.
That way you can uniquely link to that particular event in some other table, like so:
Table FavEvents:
- id: integer (autoinc primary)
- Event_id: integer (link to your event)
- FanName: varchar(x) (name of user you loves event or what ever)
Then you can select "bill"s fav events like so:
SELECT * FROM FavEvents
INNER JOIN Events ON (FavEvents.Event_id = Event.id)
WHERE FavEvents.FanName = "bill"
I never use PHP so can't help you there, good luck.