In database i have date,start time and end time.now i want to divided these times into 15 mins difference.IF start time is 4:00 AM and end time is 6:00 AM then these times divided into 4:00 AM-(to)4:15AM min,4:15 AM-4:30 AM etc.
in the below i gave some codes.please help me or give me the procedure should i follow to solved this problem
controllar code:
function fetch_availableime(){
$doctor_id = $this->input->post('doctor_id');
$date = $this->input->post('dates');
$started_time = $this->appointment_model->get_started_time($doctor_id,$date);
$ended_time = $this->appointment_model->get_ended_time($doctor_id,$date);
if ($started_time > $ended_time) {
for ($i=$started_time; $i <$ended_time ; $i++) {
$con_times = date('i:sa',strtotime('+15 minutes',strtotime($started_time)));
echo $con_times;
}
}
}
Model Code:
function get_started_time($doctor_id,$date){
$this->db->select('mstime');
$this->db->from('availability');
$this->db->where('doctor_id',$doctor_id);
$this->db->where('date',$date);
$query = $this->db->get();
return $query->result();
}
function get_ended_time($doctor_id,$date){
$this->db->select('metime');
$this->db->from('availability');
$this->db->where('doctor_id',$doctor_id);
$this->db->where('date',$date);
$query = $this->db->get();
return $query->result();
}
result() returns array of object it is better to use row() for single row then access time with object
return $query->row();
then in controller use
$started_time=$started_time->mstime;
$ended_time=$ended_time->metime;
and for compare two dates you should use strtotime(), for more info see this link
Related
I have a MySql Database with peoples' names as varchars and appointment times as DateTime objects. I need to make a function that returns the next available time slot (DateTime format) found in the database, I'll use it to schedule people for that time slot.
Appointments are only on Tuesdays and Wednesdays. Whether it's Tuesday or Wednesday, 4 people can be seen at 9:00am, and 2 people can be seen at 10:30am.
This function returns the number of people that are booked for a given time slot (DateTime).
function getTimeSlotCount($dateTime)
{
// finds out how many people are scheduled for the given datetime
global $db;
$query = "SELECT * FROM appointments
WHERE AppointmentTime = :dateTime
ORDER BY AppointmentTime";
$statement = $db->prepare($query);
$statement->bindValue(':dateTime', $dateTime);
$statement->execute();
$count = $statement->rowCount();
$statement->closeCursor();
#var_dump($count);
return $count;
}
I plan to use a do while loop to iterate through timeslots. But I don't know how to iterate through appointment times that I mentioned using DateTime. Also how to compare the time portion of a datetime to a specific time of day?
function getNextAvailableTimeSlot()
{
$maxFor9am = 4;
$maxFor1030am = 2;
$isAvailableSlotFound = false;
do {
// How Do I: Assign next Timeslot to $AppointmentTime;
$numBooked = getTimeSlotCount($AppointmentTime)
if ($numBooked >= $maxFor9am && $HowdoIGetTimeFromAppointmentTime == 9am)
continue;
else if ($numBooked >= 2 && $HowdoIGetTimeFromAppointmentTime == 1030am)
continue;
else
$isAvailableSlotFound == true;
} while ($isAvailableSlotFound == false);
return $AppointmentTime;
}
EDIT: When an appointment is added to the database table. The person's name and a DateTime is entered for the record of the appointment.
It's a simple update code in a PHP function through a GET call from the front-end(Not sure if this matters), the code is converting a particular time zone value to unix time stamps -
$oldDate = null;
$newDate = null;
$ctr = 0;
$timeStamp = array();
$query= $this->userDatabaseConnection->stmt_init();
$query->prepare("SELECT Timestamp from Transaction INNER JOIN `Order` ON `Order`.TransactionId = Transaction.TransactionId");
$query->execute();
$query->bind_result($tID); //this holds the current time zone values
while($query->fetch()){
$oldDate = $tID;
array_push($timeStamp, $oldDate);
}
$query->close();
foreach($timeStamp as $row){
$newDate = strtotime($row);
$queryUpdate= $this->userDatabaseConnection->stmt_init();
$queryUpdate->prepare("UPDATE `Order` SET OrderDate=? WHERE TransactionId=?");
error_log("ERROR!",0);
$queryUpdate->bind_param('is', $unixTS, $oldTS);
$unixTS = $newDate;
$oldTS = $row;
$queryUpdate->execute();
$queryUpdate->close();
$ctr++;
}
if($ctr == 88){
return true;
}
else{
return false;
}
}
The $ctr variable is returning the total number of rows that need to be updated - which equals to the correct value and thus returns a true to my front end Angular controller.
I tried echo-ing the $newDate and $row values - they seem to be correct too.
But it just does not update my table. Can someone please help me figure out why? Am I missing something here? The error log does show the message in my Apache log - but I can't seem to figure out what's causing it.
I want to write a function which is able to return the sum of the total_excl column at the MySQL database. I’ve written a function, but its not reusable.
The function for the first quarter of the year.
public static function getFirstQuarterTotalExcl(){
$first_day_of_1_quarter = date('Y-01-01');
$last_day_of_1_quarter = date('Y-03-31');
$query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$first_day_of_1_quarter' AND '$last_day_of_1_quarter'";
$total = Zend_Registry::get('db')->fetchAll($query);
return round($total[0]["SUM(total_excl)"]);
}
The second function for the second quarter of the year:
public static function getSecondQuarterTotalExcl(){
$first_day_of_2_quarter = date('Y-04-01');
$last_day_of_2_quarter = date('Y-06-30');
$query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$first_day_of_2_quarter' AND '$last_day_of_2_quarter'";
$total = Zend_Registry::get('db')->fetchAll($query);
return round($total[0]["SUM(total_excl)"]);
}
As you can see. This code isn’t reusable which means that for every quarter of the year I have to write a new function. I want to make one function for every quarter. What should I do to make it reusable?
I’ve written a function, but its not reusable.
No clear on what you mean here, but what about just setting the dates as part of the interface to the function like this:
public static function getQuarterTotalExcl($first_day_of_quarter,$last_day_of_quarter){
// $first_day_of_2_quarter = date('Y-04-01');
// $last_day_of_2_quarter = date('Y-06-30');
$query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$first_day_of_quarter' AND '$last_day_of_quarter'";
$total = Zend_Registry::get('db')->fetchAll($query);
return round($total[0]["SUM(total_excl)"]);
}
Then just call it like this:
$first_quarter_results = getQuarterTotalExcl(date('Y-01-01'), date('Y-03-31'));
$second_quarter_results = getQuarterTotalExcl(date('Y-04-01'), date('Y-06-30'));
$third_quarter_results = getQuarterTotalExcl(date('Y-07-01'), date('Y-09-30'));
$fourth_quarter_results = getQuarterTotalExcl(date('Y-10-01'), date('Y-12-31'));
Pass the two dates as parameters to your general function.
Something like:
function getQuarterTotalExcl($day1, $day2)
{
$query = "SELECT SUM(total_excl) FROM ".Zim_Properties::getTableName('Invoice')." WHERE invoice_date BETWEEN '$day' AND '$day2'";
$total = Zend_Registry::get('db')->fetchAll($query);
return round($total[0]["SUM(total_excl)"]);
}
I can insert data into the database using CodeIgniter and also get the data from the database, but the problem is I can't get the data from date 1-9 (the data is present in the database). After the 9th of the month, the data is retrieved successfully.
Controller:
function display($year=null,$month=null){
if (!$year) {
$year = date('Y');
}
if (!$month) {
$month = date('m');
}
$this->load->model('Calendar_model');
if ($day = $this->input->post('day')) {
$this->Calendar_model->add_calendar_data("$year-$month-$day",$this->input->post('data')
);
}
$this->load->model('calendar_model');
$data['calendar']=$this->calendar_model->generate($year,$month);
$data['viewName']=('Student/s_calendar');
$this->load->view('Student/template',$data);
}
Model:
function get_cal_data($year,$month){
$query=$this->db->select('date,data')->from('student_calender')->like('date',"$year-$month")->get();
$cal_data=array();
foreach($query->result() as $row){
$cal_data[substr($row->date,8,2)]=$row->data;
}
return $cal_data;
}
use this function for your calendar data the codeignitor only picks values past 9 because it compares the two characters in the html page to the two characters of days from your database. any number below 10 does not match because in your database it saves any day below 10 with a zero. use this
function get_calender_data($year,$month)
{
$query = $this->db->select('date, data')->from('calendar')
->like('date',"$year-$month",'after')->get();
$cal_data = array();
foreach ($query->result() as $row) {
if(substr($row->date,8,1)==0)
{
$cal_data[substr($row->date,9,1)] = $row->data;
}
else{
$cal_data[substr($row->date,8,2)] = $row->data;
}
}
return $cal_data;
}
I had the same problem via zend framework finally I got the point that the data in mysql is in this format :
YYYY-MM-DD HH:ii:ss
And my query was in this format :
YYYY-m-d h:i:s
It means for example I hade
2013-01-02 13:02:30
in database and I searched for time between
2013-1-2 13:2:30 and 2013-1-3 13:2:30
which gave me wrong answers.
use vardump and see your values of year and month!
In my database I have a table of "weeks" and a table of "workouts". I would like to achieve something like the following:
Week 1
workout 1 title
workout 1 content
workout 2 title
workout 2 content
workout 3......
week 2
workout 1 title
workout 1 content
workout 2 title
workout 2 content
workout 3......
week 3.....
I've trying something like:
function get_weekly_content() {
$user_id = $this->session->userdata('id');
$weeks = $this->db->select('week_no')->where('user_id', $user_id)->get('weeks');
if($weeks->num_rows > 0) {
foreach($weeks->result() as $row) {
$week_no = $row->week_no;
$workouts = $this->db->where('user_id', $user_id)->where('week_no', $week_no)->get('workouts');
if($workouts->num_rows > 0) {
foreach($workouts as $workout) {
$workout_data[] = $workout;
}
}
$weekly_data[] = $workout_data;
}
return $weekly_data;
}
}
but maybe I have the wrong idea. I would then also need to display the data.
EDIT my question is, what would be the best way to achieve the above and get and array/object to loop through on a view page?
Thanks
Looping with in a loop is just fine, and your approach is appropirate. However!
Generally speaking you shouldn't run mysql queries inside of loops if there is any way to avoid it because queries are quite slow this will really degrade the performance of your script. Otherwise, the idea is correct. Try to write a query outside of the loops that puts all the relevant user data into a keyed array and then pull the information from the array while you're looping.
You're going to see problems with this line: $weekly_data[] = $workout_data; if $workouts->num_rows > 0... if it's the first loop, it will throw an error because $workout_data won't be set, and if it's subsequent loops then on any week that doesn't have results, it will display the previous week's data. You can fix this by setting/resetting $workout_data at the top of the inner loop:
$workout_data = array();
if($workouts->num_rows > 0) {
foreach($workouts as $workout) {
$workout_data[] = $workout;
}
}
function get_weekly_content() {
$user_id = $this->session->userdata('id');
$weeks = $this->db->select('week_no')->where('user_id', $user_id)->get('weeks');
$weekly_data = array();
if($weeks->num_rows > 0) {
foreach($weeks->result() as $row) {
$workout_data = array();
$week_no = $row->week_no;
$workouts = $this->db->where('user_id', $user_id)->where('week_no', $week_no)->get('workouts');
if($workouts->num_rows > 0) {
foreach($workouts as $workout) {
$workout_data[] = $workout;
}
}
$weekly_data[$week_no] = $workout_data;
}
}
return $weekly_data;
}
Then access the data:
$weekly_data = get_weekly_content();
foreach( $weekly_data as $week_no => $workout_data){
// do something with it.
}
That being said, I agree with #Ben D, you should think about your query and create one that can return all the results at once.