Delete ACF Field from the backend with expired date - php

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.

Related

PHP Make form field read-only conditionally based on date & time

I'm trying to have 2 fields in my form become read only after a certain time & date.
I've cobbled together some code from a few places in an attempt to make this work but I'm a total novice so I can't identify where it's going wrong or why.
The fields I want conditionally read-only are 1 & 13 in the example below. I'm not even sure what the numbers 9 & 2 are for. Like I said, absolute beginner.
Adding Angel's time & date definitions from his answer, here's what I have so far...
$W1F1Start = "2020-10-16 12:42:00";
$now = date('Y-m-d H:i:s');
if(strtotime($now) > strtotime($W1F1Start)){
add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
if ( in_array( $field->id, array( 1,13 ) ) ) {
$values['read_only'] = 1;
}
return $values;
}
return $values;
}
If I get this to work, I eventually need to make up to 20 fields read-only at different times and dates in the same form.
My form is a Formidable form on wordpress if it makes a difference.
In their knowledge bank they have 2 bits of code that I assume I can put together somehow to do what I want, but I can't figure out how...
This one 'makes fields read only' based on a thing
add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
// If on the back-end, keep fields editable
if ( FrmAppHelper::is_admin() || current_user_can( 'administrator' ) ) {
return $values;
}
// If on front-end, make specific fields read-only
if ( in_array( $field->id, array( 554,555,556 ) ) ) {
$values['read_only'] = 1;
}
return $values;
}
And this one does something 'based on current date'
add_filter('frm_setup_new_fields_vars', 'remove_field_option_by_date', 30, 2);
add_filter('frm_setup_edit_fields_vars', 'remove_field_option_by_date', 30, 2);
function remove_field_option_by_date( $values, $field ) {
$today = time();
$close_date = strtotime ( "2020-06-20" ); // change 2020-06-20 to the date after which the option should be removed
if ($today id == 13677 ) { // change 13677 to your field id
$options_to_remove = array( 'Option 1' ); // change Option 1 to the value to remove
foreach ( $options_to_remove as $remove ) {
$option_key = array_search( $remove, $values['options'] );
if ( $option_key !== false ) {
unset( $values['options'][ $option_key ] );
}
}
}
return $values;
}
I need to 'make fields read only'-'based on a date'. But I'm at a loss as to how to put those two things together.
Hope this is all the information needed. Is this possible?
1st - in Wordpress "code snippets" go in functions.php, located inside the theme you're using, so can do this without a plugin. Now to compare 2 dates you can have something like this:
// Static W1F1 start date
$W1F1Start = new DateTime();
$W1F1Start->setTimestamp(1602840600);
$now = new DateTime();
if($now > $W1F1Start){
// do whatever
}
Or lets assume your variable $W1F1Start is a string "2020-10-16 09:30:00" (this is the value of formatted $W1F1Start->format('Y-m-d H:i:s')) and you want to compare that to what current time, then you can use strtotime(), which will convert your strings to timestamps:
$W1F1Start = "2020-10-16 09:30:00";
$now = date('Y-m-d H:i:s');
if(strtotime($now) > strtotime($W1F1Start)){
// do whatever
}
So this is your code final look:
add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
// Static W1F1 start date
$W1F1Start = new DateTime();
$W1F1Start->setTimestamp(1602840600);
// current time
$now = new DateTime();
$now->setTimezone(new DateTimeZone('UTC'));
// if cuurent time > W1F1Start
if ($now > $W1F1Start) {
if ( in_array($field->id, array(1,13))) {
$values['read_only'] = 1;
}
return $values;
}
return $values;
}

Date & time comparison operators >= not showing dates that are equal

I am trying to show events that occur either today or on a later date where today is specifically the problem.
public function getInspirationsMeetingIds()
{
$ids = [];
if (($inspirationMeetings = $this->getCustomField('meetings'))) {
foreach ($inspirationMeetings as $meeting) {
$row = new Inspiration($meeting['meeting']);
$dateFrom = $row->getCustomField('date');
if (strtotime($dateFrom) >= time()) {
$ids[] = $row->getId();
}
}
}
return $ids;
}
For some reason this will only show events that are greater than time() and not the events that are today, but then when i try this:
if (strtotime($dateFrom) <= time()) {
$ids[] = $row->getId();
}
Today's and older events are shown.
I think you need to add a timestamp to your datefrom.
Strtotime will add noon if time is omitted.
See this example https://3v4l.org/cYKO4
if (strtotime($dateFrom ) >= strtotime(date("Y-m-d 00:00"))) {
Will make it show all of datefrom
Edit added the 00:00 at the wrong side
Use the DateTime class http://php.net/manual/en/class.datetime.php
time() gives seconds since Jan 1st 1970. The chance that you hit the exact second is very small, so it will hardly ever match.
Instead, create a date with the time.
$date = new DateTime($dateFrom); // or DateTime::createFromFormat($format, $dateFrom);
$today = new DateTime();
if ($date >= $today) {
// should work
}

Calculate from exist table

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);

Php custom date format and comparison

In my database I have one table in which I keep registered users.
One column is Date of register and I keep this value in my own string format.
For example "[2013-11-30] [19:42:46]"
Then I want to make a check.
If user is 30 days old or more.
The sure thing is that the above code is wrong.
The problem is that if one user registers at 29/01/2015
will not been showing in 30 last days if the current day is 02/02/2015!
//Datetime
$today = date_parse_from_format("[Y-m-d] [H:i:s]", gmdate("[Y-m-d] [H:i:s]"));
$store = date_parse_from_format("[Y-m-d] [H:i:s]", $row["LastSeen"]);
if (
(($store[year] >= $today[year]) && ($store[month] >= $today[month]))
)
{ $date_last = "<font color='green'>".$row["LastSeen"]."</font>"; }
else
{ $date_last = "<font color='red'>".$row["LastSeen"]."</font>"; }
Use date_create_from_format instead of date_parse_from_format. Then you can simply compare the resulting values:
$today = date_create_from_format("[Y-m-d] [H:i:s]", gmdate("[Y-m-d] [H:i:s]"));
$store = date_create_from_format("[Y-m-d] [H:i:s]", $row["LastSeen"]);
if ($store < $today) {
// ...
}
else {
// ...
}

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');

Categories