1

I have a custom field, attached to a custom post The custom post is a profile (user) the Custom field a date, stored as a unix timestamp.

I evaluate like this:

function born_today_func( $atts ) {
extract(shortcode_atts(array(
    'birthday' => get_post_meta(get_the_ID(), 'wpcf-your_cf_slug', true),

    ), $atts)
);

if((date('d', strtotime($birthday)) == date('d')) && (date('m', strtotime($birthday)) == date('m'))){
    return 1;
}else{
    return 0;
}
}
add_shortcode( 'born-today', 'born_today_func' );

It does not matter if I use this as a short code, as a function or whatever, the line:

if((date('d', strtotime($birthday)) == date('d')) && (date('m', strtotime($birthday)) == date('m')))

Does not correctly evaluate the date of custom field with the current date.

I have following results for TODAY:

Profile 1

12/05/08

person 2

11/05/08

Person 3

01/05/15

But obviously only 12/05 (Profile 1 ) should be echoed.

It is worth to mention that this are NOT my only profiles, but all other dates are NOT returned (as example 13/05/08 is NOT returned)

What am I doing wrong?

How do I correctly check a UNIZ TIMESTAMP for dd/mm and output ONLY if dd/mm is corresponding with TODAY?

I am not interested in including the 29th February, thats a entire other matter.

My code is correct, it evaluates SOMETHING, but not the EXACT what it should.

I hope somebody can help.

FYI: This returns the EXACT SAME results as my above code:

$birthDate = get_post_meta(get_the_ID(), 'wpcf-birthday', true);
$time = strtotime($birthDate);
if(date('m-d') == date('m-d',$time)) {
    return 1;
}else{
    return 0;
}

Additional info suggested by Luke:

var_dump returns:

$birthDate:

string(10) "1210550400" string(10) "1421020800" string(10) "1210464000" string(10) "1430438400"

$time:

 int(-49532903345) bool(false) int(64072123846) int(202922721043) 

not sure what I can do with that now?

Regardless all (surely) helpful comments and answers here, the solution is much simpler. just do not convert.

this code will do his job as expected:

function born_today_func( $atts ) {
extract(shortcode_atts(array(
    'birthday' => get_post_meta(get_the_ID(), 'wpcf-birthday', true),

), $atts)
       );

if(((date('d', $birthday)) == date('d')) && (date('m', $birthday)) == date('m')){
    return 1;
}else{
    return 0;
}
}
add_shortcode( 'born-today', 'born_today_func' );
  • Related: http://stackoverflow.com/questions/3873649/php-birthday-check-today´s-date . Try doing `var_dump()`s throughout to spot where things are the same/different? – Luke May 13 '15 at 02:24
  • Why two separate date functions instead of just date('md') ? – Devon Bessemer May 13 '15 at 02:26
  • because, as you see, they are just 2 examples. Either if I do date(md) or date(m) and date(d) it returns both times a mixed result I will try the thing you suggest luke, but I don't see how this should solve this relatively odd thing –  May 13 '15 at 02:31
  • Luke, that post I read already and it does not solve this situation. In fact the code there does simply not work –  May 13 '15 at 02:32
  • var_dump for $birthDate returns: `string(10) "1210550400" string(10) "1421020800" string(10) "1210464000" string(10) "1430438400" ` and var_dump for $time obviously the same : ` string(10) "1210550400" string(10) "1421020800" string(10) "1210464000" string(10) "1430438400"` not sure what I can do with that now? –  May 13 '15 at 02:36
  • What date format does `strtotime` think you are using _dd/mm/yy_ or _mm/dd/yy_? – Ryan Vincent May 13 '15 at 02:44
  • as custom field is stored as TIMESTAMP, I output whatever you/I like. For now, I output dd/mm .... UNIX TIMESTAMP uses something else, such as "1431485181" for 05/13/2015 @ 2:46am (UTC). There fore, I think there is no such thing as Dateformat in UNIX Timestamp? –  May 13 '15 at 02:47
  • Was referring to the date stored in the profile. – Ryan Vincent May 13 '15 at 02:47
  • the profile uses a custom field, (date picker) this value is in DB as UNIX datestamp. the Datepicker prints (in the admin) : "May 12, 2008" –  May 13 '15 at 02:50
  • Why use a unix timestamp to store a birthdate value to begin with? – Mike Brant May 13 '15 at 02:55
  • please, I am not into discussing why there are peoples using TIMESTAMP and others don't :) It is, unfortunately, how the Custom Field is stored. I can not change that. I can change the above, but not how the fields values are stored. that comes from a WP Plugin. I know it is not the best, but it should be possible to evaluate also a UNIX TMS, don't you agree? :D –  May 13 '15 at 02:57
  • If you have a 'timestamp' then i suggest creating dates using _'DateTime::createFromFormat('U', $timestamp)'_ - [Returns new DateTime object formatted according to the specified format.](https://php.net/manual/en/datetime.createfromformat.php) or _`$date->setTimestamp($timestanp)`_ - [Sets the date and time based on an Unix timestamp](https://php.net/manual/en/datetime.settimestamp.php) – Ryan Vincent May 13 '15 at 03:25

3 Answers3

0

You should use DateTime::createFromFormat() instead of strtotime to parse date. Your date 12/05/08 looks ambiguous.

Tuan Anh Hoang-Vu
  • 1,994
  • 1
  • 21
  • 32
  • not sure how to use this? As Iam not english speaker I also have to google "ambiguos" :D –  May 13 '15 at 02:51
  • BTW, 12/05/08 is only what I decided to output. I can change that however I want per short code attribute. the problem is, the evaluation d==d and m==m –  May 13 '15 at 02:59
0
if (date('d/m', $dob) == date('d/m', time())) {
    echo 'Happy Birthday';
    echo 'You are ' . (date('Y', time()) - date('Y', $dob));
}
Enigma Plus
  • 1,519
  • 22
  • 33
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P May 29 '22 at 10:53
-1

bump post if someone is looking for simple solution, not for wordpress

$birthday = new DateTime("05-28-2020");
$today = new DateTime(date("Y-m-d"));

if ($birthday->format("m-d") == $today->format("m-d")) {
 echo 'Today is your birthday';
} else {
 echo 'Today is not your birthday';
}
mhermano
  • 89
  • 9