-3

I need to get current time in UTC to store in in datetime field(2019-08-17 00:00:00) type in MySQL.

How to get it in PHP?

I tried: now(); it gives me milliseconds

POV
  • 11,293
  • 34
  • 107
  • 201

3 Answers3

2

You should find info about the DateTime class to work with dates in php

$d = new DateTime('now');
$d->setTimezone(new DateTimeZone('UTC'));
echo $d->format('Y-m-d H:i:s');
1

You should try this.

date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s');
// Output will be : 2019-08-16 09:41:24
Bhavesh Patel
  • 115
  • 1
  • 2
  • 12
0

try this code

$date=date_create(date("Y-m-d h:i:s"));
echo date_format($date,"Y-m-d H:i:s");
#output : 2019-08-16 09:49:20
  • This is a really convoluted way to do it, full of redundant from/to string conversions, but most importantly, it's incorrect because it doesn't take time zone into account. – Álvaro González Aug 16 '19 at 11:45