Home > Blockchain >  How to set JWT refresh token expiry in days from mysql Php
How to set JWT refresh token expiry in days from mysql Php

Time:05-30

I want to set the JWT Refresh token expiry based on the MySQL date-time value. expiry date coming from MySQL which is in the format

$usr_event_data["allocexpdt"]  = 2022-05-30 00:00:00.000000
//this is how I am having it right now. its always giving token expired.
$iat = time(); 
$nbf = $iat   10;//10 seconds
$exp = $iat   30;//30 seconds $usr_event_data["allocexpdt"];
$aud = "myusers";

I want the refresh token to expire exactly on 30-05-2022 at 11:59:59 no matter what the time section of the datetime from the mysql it should always expire at 11:59:59 of that date.

CodePudding user response:

örnek kodlar ve açıklamalası içinde. inşallah işinizi görür.

<!DOCTYPE html>
<html>
<body>

<?php
date_default_timezone_set('Europe/Istanbul');
$mysql_time_stamp = strtotime("30-05-2022 11:23:00"); # mysql den gelen date
$simdiki_zaman = time();
$aradaki_fark = ($simdiki_zaman-$mysql_time_stamp);
echo $aradaki_fark; # eğer - ise henüz belirtilen zaman henüz gelmemiştir.
?>

</body>
</html>

CodePudding user response:

Remember, the PHP time() function returns the time NOW, which is of no use to you.

Probably the simplest thing to do would be to get the Query to provide the date data in the format you want it in.

SELECT .,.,., UNIX_TIMESTAMP(DATE(allocexpdt) 1)-1

That says get only the data part '2022-05-30' of the column containing 2022-05-30 00:00:00.000000 add 1 day to that date, and then subtract one second from that, getting you the 23:59:50 time part.

Alternatively in just PHP

$from_mysql = "2022-05-30 00:00:00.000000";

$dat = substr($from_mysql, 0, 10);          //2022-05-30
$dat .= ' 23:59:59';                        //2022-05-30 23:59:59

//using the DateTime class convert the date time to milliseconds since epoch
$dt = new DateTime($dat);
echo $dt->getTimestamp();            //1653955199 second since epoch

// you may now need to multiply by 1000 as javascript dates are held in millisecond
echo $dt->getTimestamp() * 1000;            //1653955199000
  • Related