Calculate from exist table - php

I want to count data from exist table which the date is today. And everyday it counts automaticaly.
I have pembayaran table that include 'tanggal' column as date and 'total' column as number that i will count.
I've tried this code. But it always give me '0'. Did i forget something?
Controller
public function index(){
$today = date('Y-m-d');
$where = array('tanggal' => $today);
$getpem = $this->aruskas_m->selectX('pembayaran',$where)->result();
$jumlah =0;
foreach ($getpem as $row) {
$jumlah += $row->total;
}
$data['kasmasuk'] = $jumlah;
$this->load->view('laporan/aruskas_v', $data);
}

Change
$where = array('tanggal' => $today);
to
$where = array('date(tanggal)='. $today);

Please make sure your date field's format is exactly like this Y-m-d format. Otherwise you have to change the date format of $today perfectly matching with tanggal field in your condition $where = array('tanggal' => $today);

Related

Delete ACF Field from the backend with expired date

I have an ACF field called sub seminars, inside this is another field called sub seminars which is a repeater field containing start_date and end_date.
I have posts that have several rows of this field.
I want to delete the row of the repeater field which has an expired date in it from the backend so that it doesn't show up in the front end.
I am using this function to achieve this but somethings are not working.
add_filter('acf/load_value/name=repeater_field_name', 'delete_old_courses_by_date');
function delete_old_courses_by_date($rows, $post_id, $field) {
if (!is_array($value) || !count($value)) {
return $value;
}
// get the current timestamp
$now = time();
// set up a new array to hold the values we keep
$new_value = array();
foreach ($rows as $row) {
// the php strtotime() function could fail depending on
// the return format of the date/time fields
// this requires a valid date/time format as documented here
// http://php.net/manual/en/datetime.formats.php
// if this does not work I probably won't be much help figuring
// our how to covert your return value to something usable
$start = strtotime($row['start_date']);
$end = strtotime($row['end_date']);
if ($start > $now || $end > $now) {
$new_value[] = $row;
}
}
return $new_value;
}
If I put repeater_field_name as start_date, all the start_date rows are deleted.
Please not my date format is Ymd and I don't know if the format is compatible with strtotime() function
Any help would be really appreciated.
This is what finally worked for me
<?php for($i=0;$i<10;$i++){
$ap = get_post_meta($post->ID,'sub_seminars_'.$i.'_start_date',true);
$startdate = date("Ymd", strtotime($ap));
$todaydate = date("Ymd");
if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
$del_data = array(
'Ref' => 'sub_seminars_'.$i.'_ref',
'Start date' => 'sub_seminars_'.$i.'_start_date',
'End Date' => 'sub_seminars_'.$i.'_end_date',
'Venue' => 'sub_seminars_'.$i.'_venue',
'Fees' => 'sub_seminars_'.$i.'_fees',
'CPE Credits' => 'sub_seminars_'.$i.'_cpe_credits'
);
delete_row('sub_seminars', 1);
}
} ?>
Hope this helps to someone else.

how to determine if selected date and time is within the range of the duration

How to check if my selected date and time is within the duration (it is the minutes)?
Here's my code:
function checkTimeDate(){
$time = $this->input->post("time", true);
$this->db->select("time, duration");
$this->db->from("timetable");
$query = $this->db->get();
$message = array();
$timeConverted = strtotime($time);
foreach($query->result() as $row){
if($timeConverted == $row->time){
$message = array(
"message" => "Please pick another date and time"
);
}else{
$message = array(
"message" => "Available"
);
}
}
$this->output->set_output(json_encode($message));
}
In that logic, Im just checking if the selected time and date is equal to the value in my database. I want to add a duration. thanks in advance.
I believe you are looking for something like this:
if $timeConverted >= $row->time && $timeConverted <= $ row->time + $row->duration
try this
$this->db->select("*");
$this->db->from("tbl_name");
$this->db->where("now() >= DATE_SUB(selected_date,INTERVAL 30 DAY)");

Codeigniter: Return true if dates are NOT between 2 values

I am using codeigniter to build a rental website for a client. I am trying to return true only if no rental periods match my query. Basically there is a rental table with start date and end date.
My customer selects a start date from a datepicker and the end date is a set number of days after the start date.
So I want to find out if any rental items are being rented on those dates to verify wether the client can reserve the item. Here is what I have so far and I need to accomplish this with Codeigniters active record...
function check_product($periodLength) {
//This is the start time they chose from the picker
$startTime = $_POST['date'];
//The period length is a variable of days, so the end date would be their chosen start date plus a certain amount of days...
$endTime = strtotime('+'.$periodLength.'day', $startTime);
$this->db->where('productsId', $_POST['productId']);
//This is where I need help!!! What query would give me records that are NOT between the start and end dates that i'm wanting
$query = $this->db->get('rentals');
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'customerId' => $row->customerId,
'productsId' => $row->productsId,
'periodStart' => $row->periodStart,
'periodEnd' => $row->periodEnd,
'status' => $row->status,
'specialInstructions' => $row->specialInstructions,
'adminNotes' => $row->adminNotes
);
}
return $data;
}
Most of my problem is just in my head i'm sure but i need to figure out if my startdate to enddate period is already reserved.
Try this:
$startTime = $_POST['date'];
$endTime = strtotime('+'.$periodLength.'day', $startTime);
$this->db->where('productsId', $_POST['productId']);
$this->db->where(" $startTime NOT BETWEEN periodStart AND periodEnd AND $endTime NOT BETWEEN periodStart AND periodEnd OR ($startTime < periodStart AND $endTime > periodEnd) ");
//Since this is a complex where clause i would recommend
//to put enitre clause in single where statement rather than
//putting in different where statement.
//And it is upto codeigniter active record standards
$query = $this->db->get('rentals');

Date Query with Doctrine

I have fields in my table for date, but they contain everything - day, year and month. Can I write a query to get only the records, which has month equal to the current month? I can do this:
$today = new \DateTime();
$month = $today->format('m');
$cat = $em->getRepository('EMBudgetTrackerBundle:Expense')->find(1);
$ex_date = $cat->getDate();
and compare $month and $ex_date, but can I write some kind of query? Something like this:
public function getExpensesByMonth($month)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date = :date')
->setParameter('date', $month);
return $q->getQuery()->getResult();
}
Thank you in advance! :)
If you database column is in DateTime format you can use the DateTime object in your query. As far as I know you can only query for time ranges though.
public function getExpensesByMonth($beginning, $end)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date > :beginning')
->andWhere('e.date < :end')
->setParameter('beginning', $beginning)
->setParameter('end', $end);
return $q->getQuery()->getResult();
}

Find Earliest Date - PHP

I use PHP to perform SQL to pull out some data from my database between a date range. The dates are stored as date in the relation:
$from = "2011-08-11";
$to = "2011 - 08- 25";
$query = mysql_query("SELECT date FROM `entries` WHERE date BETWEEN '$from' AND '$to' ORDER BY date ASC");
I would like to find the earliest date pulled from the relation.
If the query is successful, I store the 'date' attribute in a php array called $dates. I thought I could iterate over this $dates and compare the $date values after converting them into dates.
if($query){
$dates = array();
while($row = mysql_fetch_array($query)){
$dates[] = $row['date'];
}
$min = strftime("%Y-%m-%d", strtotime($dates[0]));
for($i = 1; $i < count($dates); $i++){
if($dates[i] < $min){
$min = $dates[i];
}
}
This does not work however...It prints random values....Perhaps there is a much simpler way to do this and I am overcomplicating matters...
HEEEELLLP!!
If you order your query, then it will be the first (or the last) row in you're query. So you wouldn't need to find it.
Instead of
$min = strftime("%Y-%m-%d", strtotime($dates[0]));
you should use
$min = date("%Y-%m-%d", strtotime($dates[0]));
The simplest way is to use the first date since you know it is already the earliest due to ASC in your SQL statement. After you read the rows into your array, just use the first element.
while($row = mysql_fetch_array($query)){
$dates[] = $row['date'];
}
$earliest_date = $dates[0];
If all you want to do is find just the earliest date, and you don't care about the rest, you could use the aggregate function min() in your query like so:
SELECT MIN(date) AS earliest FROM `entries` WHERE date BETWEEN '$from' AND '$to'
Then just grab the earliest column from the result set in your php code.
Convert the dates to numbers and sort the array?
Take what you have (lines 1-5),
foreach ($dates as $date) {
// convert each date from "YYYY-MM-DD" to "YYYYMMMDD" and turn it into an int
$date = intval(str_replace("-", "", $date));
}
// sort the array from lowest to highest
asort($dates);
// the minimum is the first item in the array
$minint = $date[0];
// convert back into "YYYY-MM-DD" format
$min = substr($minint, 0, 4) . "-" . substr($minint, 4, 6) . "-" . substr($minint, 6);

Categories