Score:1

how to convert date into timestamp using timezone

ls flag

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 }}
my flag
That DB query is open to SQL injection. Please read https://www.drupal.org/docs/drupal-apis/database-api/static-queries#placeholders for how to safely handle the parameters.
Score:2
cn flag

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();
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.