PHP - Months In Selected Date Range.
I am working on a report generation. Here I need to count the number of months involved in the selected date range. I need to apply the monthly charges accordingly.
Example: If the user selects 25-08-2011 to 03-10-2011, it should return 3 as the number of months. Yes, the number of days are less than 60 but still the date range is spread across 3 months. Any solution.. Please. Girish Similar TutorialsI'm getting this error "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/westiehi/public_html/groupBOS.php on line 36" for this code: Code: [Select] $query_rs_specialty = "SELECT * FROM specialty WHERE Group_Place <> '' and Date>= DATE_SUB(CURDATE(), INTERVAL 6 MONTH) ORDER BY Date DESC"; I've tried everything to get this to take records where the one field is blank and the date is greater than a date 6 months ago but nothing seems to work. Help would sure be appreciated!! So this is a simple code that finds out the difference between two dates and displays it in number of days. $date1=date_create("2013-03-15"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2); echo $diff->format("%R%a days"); // RESULT +272 days
My first question. Is it possible to remove the + sign in the result above? Second question. Is it possible to show "months" if it's greater than 30 days? And years if the days are greater than 365? How would I do this? I am using the following code to get the third sunday of every month for +6 Months from todays date. I have put together the following code which works perfectly when the date is echoed however when it comes to inserting those dates into mysql db it throws a unable to convert to string error. Im new to this and my head is in a spin as to why those dates can't be inserted if they can echoed ok. I have spent 2 days at it now and searched many forum however they seem to get side tracked from my issue (or do they)??????? I figure I can't go mixing DateTime and date() the way I have? Please help??? Code: [Select] [php] $month=date('F'); $now=date('Y-m-d'); $year=date('Y'); $num='3'; //example only this will be a $_POST value 0-3 $day='Sunday'; //example only this will be a $_POST value Monday-Sunday $start= date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.'')); if($start>$now) { $begin=strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.''); } else { $d = new DateTime( $start ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $year = $d->format( 'Y' ); $begin=strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.''); } $date=date('Y-m-d', $begin); # insert into db first occurance //mysql_select_db($database); //$query_rsFirst = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsFirst = mysql_query($query_rsFirst) or die(mysql_error()); echo $date.'<br/>'; for($i = 0; $i <= 4; $i++) { $d = new DateTime( $date ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $date=date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.'')); # insert into db //mysql_select_db($database); //$query_rsDate = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsDate = mysql_query($query_rsDate) or die(mysql_error()); echo $date.'<br/>'; if($nextmonth=="December") { $d = new DateTime( $date ); $d->modify( 'first day of next year' ); $year = $d->format( 'Y' ); } } [/php] Why is this not working? Code: [Select] $result = mysql_query("SELECT * FROM contacts WHERE type = 'consumer' AND sent BETWEEN '".date("Y-m-d")."' AND '".date("Y-m-d", strtotime("-7 days"))."'") or die(mysql_error()); $numrows = mysql_num_rows($result); if($numrows < 0 ){ echo "No records found"; } The record that is stored in the sent column is like this "2011-11-03 14:42:12", so there is a record in there, but nothing is showing up. Can anyone see why? thanks in advance I am currently working on a date range script and wondering if anybody think of an easier way to do this, doing up to next 8 weeks? This one starts on a Sunday. I also need to do one starting on a Monday (will figure that one later) $thisweek = date("Y-m-d"); $week = date('w', strtotime($thisweek)); $date = new DateTime($thisweek); $firstWeek = $date->modify("-".$week." day")->format("M d"); $endWeek = $date->modify("+6 day")->format("M d"); echo $firstWeek." - "; echo $endWeek; $nextweek = date(("Y-m-d"), strtotime("+7 Days")); $week1 = date('w', strtotime($nextweek)); $date1 = new DateTime($nextweek); $firstWeek1 = $date1->modify("-".$week1." day")->format("M d"); $endWeek1 = $date1->modify("+6 day")->format("M d"); echo $firstWeek1." - "; echo $endWeek1;
Hi i have the following code Quote
$sql= "SELECT * FROM income WHERE month(date) between '04' and '12' and year(date) between 2020 and 2020"; This obviously selects all the information from April 2020 to December 2020
How do i change this to add a date as well for example if i wanted to display all the information from April 6th 2020 to December 5th 2020 ?
Hello, I'm very disapointed because I don't know how do it this. I'm explain. I have a database with one input date_arrival and second date_departure. Two dates are saved in SQL format : 2014-03-02. After my SQL request to select all dates in database I'd like to add dates between date_arrival and date_departure and for each line. For example I have date_arrival date_departure line 1 : 2014-02-01 2014-02-05 line 2 : 2014-02-10 2014-02-15 In fact at the end, I have to send the result like this (with JSON) : ["2014-02-01","2014-02-02","2014-02-03","2014-02-04","2014-02-05","2014-02-10","2014-02-11","2014-02-12","2014-02-13","2014-02-14","2014-02-15"] Can you give me some help to add intermediates values in an array ? Thx Setup: Mysql table 1: acct#, client name, status(active/not active), Starting balance, ending balance Mysql table 2: acct#, invoice amount, invoice date, total how do i select all customers that are Active, and the starting balance != ending balance and then get the total for all invoices for that customer where the invoice date is between 2010-01-01 and 2010-03-01 Basically my out put needs to look like this. Customer name, Total of all invoices for customer john doe, $10,564.21 Susy Scott, $158.02 Bang Head, $837,294.85 Customer can have multiple invoices within the given date range. But I only need the total for all the invoices for each customer added up and echo'ed. Quickness is also a factor on the query and echo. Thank you for your valuable time. I'm trying to wrap my head around how i should go about doing this. I have two dates...2011-05-03 and 2011-05-08. I want to write a function that creates an array of the days that consist of that date range. So say something like Code: [Select] <?php $start = "2011-05-03"; $end = "2011-05-08"; function get_days($start, $end) { //code to get the days } echo get_days($start, $end); //hopefully produce something like... $day['1'] = "2011-05-03"; $day['2'] = "2011-05-04"; $day['3'] = "2011-05-05"; $day['4'] = "2011-05-06"; $day['5'] = "2011-05-07"; $day['6'] = "2011-05-08"; anybody know any idea how to do something like this? Pretty sure i'm going to need the mktime() function to account for ranges going across months and years. Hi all
This is a one I've spent DAYS on and can't figure out :-\ This is for a wedding venue and their pricing schedule is divided into low, medium, high and very high seasons. I.e. low season runs from 8 Jan 16 to 28 Feb 16, medium runs from 4 Mar 16 to 20 Mar 16, etc. The database for this is laid out as attached. I would like people to be able to enter their arrival and departure date and for it to be able to choose which seasons the date range falls under. I.e. If they stay 8 to 12 Jan, that's no problem to calculate and falls under LOW season. However, if they stay 27 Feb to 10 Mar, this falls under both LOW and MID seasons, with the majority of the stay in the MID season (which we'd price with but can use PHP to choose the 'highest' season from the array). I'm a bit stumped on this one so any help is hugely appreciated! I'm sure it's something quite simple that I'm missing. Attached Files Screen Shot 2015-01-10 at 16.58.17.png 146.3KB 0 downloads Hi,
I can I include a date range criteria to query with in the following code? The date field in the table (t_persons) is IncidentDate.
$criteria = array('FamilyName', 'FirstName', 'OtherNames', 'NRCNo', 'PassportNo', 'Gender', 'IncidenceCountryID', 'Status', 'OffenceKeyword', 'AgencyID', 'CountryID', 'IncidenceCountryID' ); $likes = ""; $url_criteria = ''; foreach ( $criteria AS $criterion ) { if ( ! empty($_POST[$criterion]) ) { $value = ($_POST[$criterion]); $likes .= " AND `$criterion` LIKE '%$value%'"; $url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]); } elseif ( ! empty($_GET[$criterion]) ) { $value = mysql_real_escape_string($_GET[$criterion]); $likes .= " AND `$criterion` LIKE '%$value%'"; $url_criteria .= '&'.$criterion.'='.htmlentities($_GET[$criterion]); } //var_dump($likes); } $sql = "SELECT * FROM t_persons WHERE PersonID>0" . $likes . " ORDER BY PersonID DESC";Kind regards. I'm trying to do the following PHP. I have written it in English if (today's date) => date1 AND <= date2 then {display image1} elseif (today's date) => date3 AND <= date4 then {display image2} else {display image 13} There are 24 fixed dates and 13 fixed images. I have tried using combinations of strtotime(), replacing the date value with variables, hard coding the dates within the program. I don't seem to be able to get any combination to work properly. I'm sure it's just a syntax error but I can't see it. When I've searched the web all the answers I've found relate to dates within databases but as I only have 24 dates it seems a bit of overkill. I would appreciate any pointers to the correct method I might be able to use. The dates need only a day and month as I would like this to repeat year after year. <?php $today = strtotime(date('d-m')); if (strtotime($today) >= strtotime('28-10') && strtotime($today) <= strtotime('24-11')) {echo "<div>image1</div>";} elseif (strtotime($today) >= strtotime('25-11') && strtotime($today) <= strtotime('22-12')) {echo "<div>image2</div>" ;} elseif (strtotime($today) >= strtotime('23-12') && strtotime($today) <= strtotime('24-02')) {echo "<div>image3</div>" ;} else {echo "<div>image13</div>";} ?> The result I get from this is image1 appears on the web page but I would expect image3 as today is 22-01 Thank you in advance. Andrew Hello, Im trying to work out some code. On my site between Thursday 12th July and Sunday 15th July between the hours of 22:00 and 23:00 I want to display some code. I've done this so far, but am having trouble. Anyone please help? $the_date = date("H:i:s"); $timesep = explode(":",$the_date); // $hour = $timesep[0]; if (($the_date > 2010-08-12 && < 2010-08-15) && ($hour >= 22) && ($hour <= 23)) { //Code } I have the week date range displaying the week range from sunday - saturday
$current_dayname = date("0"); // return sunday monday tuesday etc. echo $date = date("Y-m-d",strtotime('last sunday')).'to'.date("Y-m-d",strtotime("next saturday")); Which outputs in the following format 2015-01-25to2015-01-31 However, when I press next or previous, the dates don't change. <?php $year = (isset($_GET['year'])) ? $_GET['year'] : date("Y"); $week = (isset($_GET['week'])) ? $_GET['week'] : date('W'); if($week > 52) { $year++; $week = 1; } elseif($week < 1) { $year--; $week = 52; } ?> <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 52 ? 1 : 1 + $week).'&year='.($week == 52 ? 1 + $year : $year); ?>">Next Week</a> <!--Next week--> <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week == 1 ? 52 : $week -1).'&year='.($week == 1 ? $year - 1 : $year); ?>">Pre Week</a> <!--Previous week--> <table border="1px"> <tr> <td>user</td> <?php if($week < 10) { $week = '0'. $week; } for($day= 1; $day <= 7; $day++) { $d = strtotime($year ."W". $week . $day); echo "<td>". date('l', $d) ."<br>". date('d M', $d) ."</td>"; } ?> </tr> </table> Hi. Im searching for a way to enter multiple records at once to mysql databased on a date range. Lets say i want to insert from date 2010-11-23 to date 2010-11-25 a textbox with some info and the outcome could look in database like below. ---------------------------------------------------------- | ID | date | name | amount | ----------------------------------------------------------- | 1 | 2010-11-23 | Jhon | 1 | | 2 | 2010-11-24 | Jhon | 1 | | 2 | 2010-11-25 | Jhon | 1 | ----------------------------------------------------------- The reason i need the insertion to be like this is because if quering by month and by user to see vacation days then vacations that start in one month and end in another month can be summed by one month. If its in one record then it will pop up in both months. Help is really welcome. Hello,
I've been going about reading different posts on here and other forums but so far I haven't been able to come across something that works for my purpose so after all the reading I thought I'd finally just ask. I'm rather new at php/sql queries so please bare with me.
First what I'm trying to accomplish.
I need a form with several fields (options) to fetch information from a table that will then display the results based on the options selected.
- from date
- to date
- employee name
- department
- branch
- product line
In other words, the purpose is to be able to choose a date range (from one day to up to a year or more) and then to be able to choose either ONE employee name to view statistics invididually from a given department and branch or to select a department to view all the statistics for everyone under that given department and/or brach and/or product line.
Now the tricky part is that besides fetching the records, calculations need to be made before the records are displayed and that part right here is what is giving me a huge headache.
This is the query that I have.
SELECT report_daily_id, report_date, emp_id, emp_fullname, emp_dept, emp_branch prod_line calls, tk_time, hld_time, ac_time, tran_calls, work_time, tran_rate, ah_time FROM daily_report GROUP BY emp_id, emp_fullname, emp_branch, emp_dept, prod_lineThe calculations based on the date range and one or more of the other options need to give me the following results. SUM(calls) AS 'Total Calls' SUM(tk_time) / SUM(calls) AS 'Talk Time' SUM(hld_time) / SUM(calls) AS 'Held Time' SUM(ac_time) / SUM(calls) AS 'AC Time' SUM(tran_calls) AS 'Total Trans' SUM(tran_calls) / SUM(calls) AS 'Tran Rate' SUM(ah_time) / SUM(calls) AS 'AH Time'I don't know how else to explain myself past this point but if you have any questions, perhaps I can answer it and give more details. Thank you, I'm creating a line graph using ChartJs. I wanted to display my data according to the month selected by the user. For example, if the user select "2021-03" all the order that is made in the month of March will be plotted in the graph. It works well for the current month, but it does not work for the selected month. I use Ajax to get the data but i do not know why my graph does not shown when i click on my `loadchart();` button. Can anyone enlighten me how can i do it? Because i do not know what mistake did i made here. Any kind of explanation will be appreciated. Thanks!
This is the where i place my `loadchart()` button and also the javascript to plot the graph. <div class="col-md-12 form-group"> <div class="card" style="box-shadow: 0 0 8px rgb(0,0,0,0.3)"> <div class="row"> <div class="col col-lg-1"></div> <div class="col-2 my-auto"> <input type="month" id="month" class="form-control" value="<?php echo date('Y-m'); ?>"> </div> <div class="col-2 my-auto"> <button type="button" class="btn btn-info" onclick="loadchart();"> <i class="nc-icon nc-zoom-split"></i> </button> </div> </div> <div id="loadchart"> <h6 class="text-muted text-center p-3"><i class="fa fa-line-chart"></i> Daily Gross Sales (Current Month)</h6> <div class="card-body"> <canvas id="attChart"></canvas> </div> </div> </div> </div> </div> </div> </div> </div> <!-- Chart JS --> <script src="./assets/js/plugins/chartjs.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <?php $days = array(); $gross_sales = array(); $average_sales = array(); $type = CAL_GREGORIAN; $month = date('n'); // Month ID, 1 through to 12. $year = date('Y'); // Year in 4 digit 2009 format. $day_count = cal_days_in_month($type, $month, $year); // Get the amount of days //loop through all days for ($i = 1; $i <= $day_count; $i++) { $sql = "SELECT *, SUM(purchase_price) as purchase_price FROM ordered_items WHERE DAY(order_datetime) = '$i' AND MONTH(order_datetime) = MONTH(NOW()) AND YEAR(order_datetime) = YEAR(NOW()) AND order_status = 5 "; $query = $conn->query($sql); $total = $query->num_rows; $row = $query->fetch_assoc(); if($row['purchase_price'] != 0) { $gross_sales[] = $row['purchase_price']; } else if ($row['purchase_price'] == 0 && $i <= date('d')) { $gross_sales[] = 0; } $average_sales[] = $row['purchase_price'] / $day_count; $day = $i.'/'.$month; //format date array_push($days, $day); } $days = json_encode($days); $daily_gross_sales = json_encode($gross_sales); $average_gross_sales = json_encode($average_sales); ?> <script> const colors = { colorcode: { fill: '#51cbce', stroke: '#51cbce', }, }; var ctx2 = document.getElementById("attChart").getContext("2d"); const attChart = new Chart(ctx2, { type: 'line', data: { labels: <?php echo $days; ?>, //labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], datasets: [{ label: "Gross Sales: RM", fill: true, //backgroundColor: colors.colorcode.fill, pointBackgroundColor: colors.colorcode.stroke, borderColor: colors.colorcode.stroke, pointHighlightStroke: colors.colorcode.stroke, borderCapStyle: 'dot', pointRadius: 5, pointHoverRadius: 5, pointStyle: 'dot', data: <?php echo $daily_gross_sales; ?>, showLine: true }, { label: "Average Sales: RM", fill: true, //backgroundColor: colors.colorcode.fill, pointBackgroundColor: '#FF0000', borderColor: ' #FF0000 ', pointHighlightStroke: colors.colorcode.stroke, borderCapStyle: 'dot', pointRadius: 5, pointHoverRadius: 5, pointStyle: 'dot', data: <?php echo $average_gross_sales; ?>, showLine: true } ] }, options: { responsive: true, // Can't just just `stacked: true` like the docs say scales: { yAxes: [{ stacked: false, gridLines: { display: true, color: "#dee2e6" }, ticks: { stepSize: 100000, callback: function(tick) { return 'RM'+tick.toString(); } } }], xAxes: [{ stacked: false, gridLines: { display: false }, }] }, animation: { duration: 3000, }, legend:{ display: false, } } }); </script> This is my AJAX method <script type="text/javascript"> function loadchart() { $('#spin1').show(); var month= $('#month').val(); //alert(date); var data = { // create object month: month, } $.ajax({ method:"GET", data:data, url:"includes/loadchart.php", success:function(data) { $('#loadchart').html(data); } }); } </script> This is where i use to load the graph whenever a month is selected. (loadchart.php) <?php include '../session.php'; if(isset($_GET['month'])) { $months = $_GET['month']; ?> <h6 class="text-muted text-center p-3"><i class="fa fa-line-chart"></i> Daily Gross Sales (<?php echo $months ?>)</h6> <div id="loadchart" class="card-body"> <canvas id="attChart"></canvas> </div> </div> </div> </div> </div> </div> </div> <!-- Chart JS --> <script src="./assets/js/plugins/chartjs.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <?php $days = array(); $gross_sales = array(); $average_sales = array(); $type = CAL_GREGORIAN; // Gregorian (localized) calendar $month = date('n'); // Month ID, 1 through to 12. $year = date('Y'); // Year in 4 digit 2009 format. $day_count = cal_days_in_month($type, $month, $year); // Get the amount of days //loop through all days for ($i = 1; $i <= $day_count; $i++) { $sql = "SELECT *, SUM(purchase_price) as purchase_price FROM ordered_items WHERE DAY(order_datetime) = '$i' AND MONTH(order_datetime) = MONTH('".$months."') AND YEAR(order_datetime) = YEAR('".$months."') AND order_status = 5 "; $query = $conn->query($sql); $total = $query->num_rows; $row = $query->fetch_assoc(); if($row['purchase_price'] != 0) { $gross_sales[] = $row['purchase_price']; } else if ($row['purchase_price'] == 0 && $i <= date('d')) { $gross_sales[] = 0; } $average_sales[] = $row['purchase_price'] / $day_count; $day = $i.'/'.$month; //format date array_push($days, $day); } $days = json_encode($days); $daily_gross_sales = json_encode($gross_sales); $average_gross_sales = json_encode($average_sales); ?> <script> const colors = { colorcode: { fill: '#51cbce', stroke: '#51cbce', }, }; var ctx2 = document.getElementById("attChart").getContext("2d"); const attChart = new Chart(ctx2, { type: 'line', data: { labels: <?php echo $days; ?>, //labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], datasets: [{ label: "Gross Sales: RM", fill: true, //backgroundColor: colors.colorcode.fill, pointBackgroundColor: colors.colorcode.stroke, borderColor: colors.colorcode.stroke, pointHighlightStroke: colors.colorcode.stroke, borderCapStyle: 'dot', pointRadius: 5, pointHoverRadius: 5, pointStyle: 'dot', data: <?php echo $daily_gross_sales; ?>, showLine: true }, { label: "Average Sales: RM", fill: true, //backgroundColor: colors.colorcode.fill, pointBackgroundColor: '#FF0000', borderColor: ' #FF0000 ', pointHighlightStroke: '#FF0000', borderCapStyle: 'dot', pointRadius: 5, pointHoverRadius: 5, pointStyle: 'dot', data: <?php echo $average_gross_sales; ?>, showLine: true } ] }, options: { responsive: true, // Can't just just `stacked: true` like the docs say scales: { yAxes: [{ stacked: false, gridLines: { display: true, color: "#dee2e6" }, ticks: { stepSize: 100000, callback: function(tick) { return 'RM'+tick.toString(); } } }], xAxes: [{ stacked: false, gridLines: { display: false }, }] }, animation: { duration: 3000, }, legend:{ display: false, } } }); </script> <?php } ?> Edited April 27 by sashavalentina I have this function where i want to pass the month and year according to the date drop down that is chosen by the user. But can't seem to get the selected month and year. But instead, i got the month and year for now (which is April 2020). Can any one show me how can i achive this? I mean how can i pass the month and year selected to my `allreport-summary.php` page. It would be better if you guys can show me examples of codes on how can i implement this. Any help will be appreciated. Thank you so much. cheers!
<div class="row"> <div class="col-4 my-auto"> <input type="month" id="month" class="form-control" value="<?php echo date('Y-m'); ?>"> </div> <div class="col-4 my-auto"> <select class="form-control" id="seller"> <option value="">Select All</option> <?php $sql = "SELECT * FROM sellers ORDER BY seller_registered"; $query = $conn->query($sql); while ($row = $query->fetch_assoc()) { ?> <option value="<?php echo $row['id'];?>"><?php echo $row['seller_fullname'];?></option> <?php } ?> </select> </div> <div class="col-4"> <button type="button" class="btn btn-info" onclick="loadreports();"> <i class="nc-icon nc-zoom-split"></i> </button> <a href="allreport-summary.php?month=<?php echo date('Y-m');?>"> <button type="button" class="btn btn-info" style="height: 40px;"> <i class="fa fa-file-excel-o"></i> Export All Report </button> </a> </div>
|