0

I am using strtotime to convert date into time stamp but its does not work correctly. I know its relatively to time zone. My drupal store time zone is denver. I am not sure why its not showing correct date. there is around one day difference. so The last date I used 31 but I get last date of 30th not 31

Here are my query to make date into time and fetch record

date_default_timezone_set(drupal_get_user_timezone());
$startdate = strtotime(\Drupal::request()->query->get('startdate')); // 2021-12-01
$endate = strtotime(\Drupal::request()->query->get('enddate')); //2021-12-31
$database = \Drupal::database();
if(!empty($startdate)){
$userrecord = $database->query("SELECT mail, access FROM users_field_data WHERE access BETWEEN $startdate and $endate")->fetchAll(); 

Here is twig where i Called date

{{ result.access|format_date('custom', 'F j, Y')|e }}
1

1 Answer 1

2

Drupal stores times always in UTC, never in local time. Also Unix timestamps are by definition in UTC.

You can convert a local date string to a timestamp by creating a \DateTime object:

$timestamp = (new \DateTime('2021-12-31'))->getTimestamp();

Which is using the default PHP timezone or by specifying your own time zone:

$timestamp = (new \DateTime('2021-12-31', new \DateTimeZone('America/New_York')))->getTimestamp();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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