Wordpress post time comperation

i need to separate posts older than 2017-07-01.

In loop i'm using these two codes for displaying blogposts.

$compare_date = strtotime("2017-07-01");
$post_date = get_the_date();

and trying to compare them like

if( $compare_date >= $post_date ){
    //old
}else{
    //new
}

but abviously it didnt work because one of them returns 1498867200 and other one 1 Haziran 2017 I also tried putting strtotime(get_the_date()) but that result is empty.

How can i achieve this?

Upvotes: 0

Views: 40

Answers (1)

Ravinder Reddy
Ravinder Reddy

Reputation: 3879

Get the post_date in the same format as compare_date

Now compare with dates

$compare_date = "2017-07-01";
$post_date = get_the_date( 'Y-m-d' );
if( $compare_date >= $post_date ){
    //old
}else{
    //new
}

Now compare to time

$compare_date = strtotime("2017-07-01");
$post_date = strtotime(get_the_date( 'Y-m-d' ));
if( $compare_date >= $post_date ){
    //old
}else{
    //new
}

Not tested, but workable solution.

Upvotes: 1

Related Questions