I created an PHP Applications that can be create new linux user account with auto expired after several periode.

After searching on Google about 'How to automate remove linux user after fixing periode' I found this answer, https://unix.stackexchange.com/a/80971/224583

But I didn't know how to make, for example create new account and expired after 10 months, or 5 months, etc.

My question is, how to convert month to day, example I give 5 month and convert days count of five month. Or any solutions apart of my question?

2 Answers 2


The expire option in the useradd utility requires a date specified in the format YYYY-MM-DD.

You can calculate that date using the -d option to the date command. Using your 5 month scenario example:

$ date +"%Y-%m-%d"
$ date -d "5 months" +"%Y-%m-%d"

The -e switch of useradd expects a date in the international date format (YYYY-MM-DD) as can be taken from its man page:

   -e, --expiredate EXPIRE_DATE
       The date on which the user account will be disabled. The date is
       specified in the format YYYY-MM-DD.

You now simply have to state the date the account is supposed to expire. You can do this calculation in PHP. The answer you linked to does it using a standard command line tool so no high-level language has to be used.

Here is example code of how to do it in PHP:

$diffHour = 0;
$diffMinute = 0;
$diffSecond = 0;
$diffMonth = 10;
$diffDay = 0;
$diffYear = 0;

$hour = date("H") + $diffHour;
$minute = date("i") + $diffMinute;
$second = date("s") + $diffSecond;
$month = date("n") + $diffMonth;
$day = date("j") + $diffDay;
$year = date("Y") + $diffYear;

$time = mktime($hour, $minute, $second, $month, $day, $year);

$date = date("Y-m-d", $time);

Just fill in the variables in the top-most block. I filled them in for your 10 months example. You can go back in time, too. For example, if you want it to expire in in 7 months minus 4 days, you can set $diffMonth = 7; and $diffDay = -4;.

Then, pass the date value (2018-02-09 when run today (except if you live in America, then it's a day earlier because we're still using a retarded time system with time zones)) to your bash script.

Example bash code:

useradd -e "2018-02-09" "rizal"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.