Navigation überspringen

[Gelöst] Compare the custom field date with today

This support ticket is created vor 2 Jahre, 9 Monaten. There's a good chance that you are reading advice that it now obsolete.

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 -

Zeitzone des Unterstützers: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 3 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Minesh vor 2 Jahre, 9 Monaten.

Assistiert von: Minesh.

Author
Artikel
#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
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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:
=> versteckter 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
Unterstützer

Sprachen: Englisch (English )

Zeitzone: 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";
}