Skip Navigation

[Resolved] Compare the custom field date with today

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 1 year, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2477701

Dear Sir/Madam,

I have a custom date field and I get the value using get_post_meta

$firstBillDate = get_post_meta( $post_id, "wpcf-uprise-first-bill-date", true);
printf("firstBillDate Type: %s<br/>", gettype($firstBillDate));

It returns string, should I convert this to integer and then compare with strtotime(date('Y-m-d')) or there is another method to get the value of custom date field? Please advise the best practice in comparing the date.

#2478049

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset Types custom date field stores the value to database as Unix Timestamp that is why when you fetch the value of the field from database using the WordPress native function get_post_meta() for the field "uprise-first-bill-date" it return you timestamp.

So to compare the current time:

$firstBillDate = get_post_meta( $post_id, "wpcf-uprise-first-bill-date", true);
if($firstBillDate > time() ) {
   echo "bill date is in future";
}else{
   echo "bill date is passed";
}

More info:
=> hidden link

#2478351

Dear Minesh,

Unix Timestamp should be in integer type but why the get_post_meta() return as string? Should I need to use intval() function to do the comparison ?

#2478393

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should not worry about that, the if condition I shared should work.

$firstBillDate = get_post_meta( $post_id, "wpcf-uprise-first-bill-date", true);
if($firstBillDate > time() ) {
   echo "bill date is in future";
}else{
   echo "bill date is passed";
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.