0

Let say i have identification card number in this format

 720901155172

we can convert the number to birthday by using this code

        $data = '720901155172';
        $head = substr($data, 0, 6);

        $year = (int) substr($head, 0, 2);
        $currentYear = date('Y');

        $max = (int) substr($currentYear, 2, 4);

        $byear = $year + ($year > $max ? 1900 : 2000);
        $bmth = (int) substr($head, 2, 2);
        $bday = (int) substr($head, 4, 2);

        $bdate = date('Y-m-d', strtotime(sprintf('%s-%s-%s', $byear, $bmth, $bday)));
        echo $bdate;//output : 1972-09-01

how do i select all user that is having birthday today ? is this possible ?

$sql="SELECT ic FROM users";
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
darrenx
  • 33
  • 5

2 Answers2

0

Since the birthday that you need to look at is on positions 2-5 in the ic string you can use REGEX to match it with today's date.

SELECT * FROM users WHERE ic REGEXP '^[0-9]{2}0901[0-9]{6}$';

Assuming each ic is a fixed length of 12 numbers this will match a string that:

  1. starts with 2 numbers between 0-9
  2. matches input date exactly
  3. ends with 6 numbers between 0-9

Today's date part of the REGEX you will have to insert into the query dynamically with PHP, so the code would look (depending on what framework you are using) something like:

$db->get_results("SELECT * FROM users WHERE ic REGEXP '^[0-9]{2}%d[0-9]{6}$'", $date);
Cray
  • 2,774
  • 7
  • 22
  • 32
0

What I understand is first 6 digits of your ic is YYMMDD. If this is not the case please ignore. Accordingly you need this conversion in sql.

    select ic,
    case when substring(ic,1,2) <Substring(YEAR(NOW()),-2) 
    then Concat('20',substring(ic,1,2),'-'  ,substring(ic,3,2),'-', substring(ic,5,2))
    else
     Concat('19',substring(ic,1,2),'-'  ,substring(ic,3,2),'-', substring(ic,5,2))
    end as derived_date
    from Users

please update 'Substring(YEAR(NOW()),-2)' with your $max, 2 digit year.

Deepak Kumar
  • 298
  • 1
  • 7