Unix timestamp and mysql date: date of birth

I have a really basic question regarding unix timestamp and mysql date. I am trying to create a small website where users can register and fill out their date of birth.

The problem is that unix starts on January 01, 1970. Now, if I am calculating the age for users, specify dates such as date ('mdY', $ unix_from_db), etc., this will crash users over 40 years old, right?

So what would be the only way to do this. Unfortunately for a basic question like this, but I'm inexperienced with php and mysql.

// Edit: Thank you all. I was embarrassed because the date ('mdy', mktime (0,0,0,1,1,1890)) and the date ('mdy', - 2000) came back 01.01.70 - next time I will study the manual more carefully . I was able to fix my site, thanks again. It is a pity that I can only accept one answer, they are all equally good.

+3
source share
4 answers

In dates prior to the era, the number is still increasing, thereby becoming less negative, as you move forward.

http://en.wikipedia.org/wiki/Unix_time

You should not have any problems with dates before the era:

<?php
echo date("m-d-Y", mktime(0, 0, 0, 1, 1, 1970));
echo date("m-d-Y", mktime(0, 0, 0, 1, 1, 1969));
echo date("m-d-Y", mktime(0, 0, 0, 1, 1, 1943));
?>

Outputs

01-01-1970
01-01-1969
01-01-1943
+3
source

Unix timestamps for periods prior to 1970 are negative integers, counting the number of seconds until jan 1 1970.

, 2/13/1943 - -848343600

+5

? . , , .

Negative numbers are people too! Do not distinguish! It hurts their feelings.

+3
source

Save the date as a date or date in mysql.

Print it as you like with the date:

date('<format>', strtotime($date_from_db));

strtotime will return some negative integer for dates before 1970. date will handle this just fine.

+2
source

Source: https://habr.com/ru/post/1741185/


All Articles