Is there a PHP function that returns the date and time in the same format as the MySQL function NOW()
?
I know how to do it using date()
, b
shortly
echo date('Y-m-d H:i:s');
php advanced now class extra addMinute addYear as such addHour etc ...
<?php /** @noinspection PhpUnhandledExceptionInspection */
/**
* Class Now
* @author dılo sürücü <berxudar@gmail.com>
*/
class Now
{
/**
* @var DateTime
*/
private $dateTime;
/**
* Now constructor.
* @throws Exception
*/
public function __construct()
{
$this->dateTime = new DateTime('now');
}
/**
* @param int $year
* @return Now
* @throws Exception
* @noinspection PhpUnused
*/
public function addYear(int $year): self
{
$this->dateTime->add(new DateInterval('P' . $year . 'Y'));
return $this;
}
/**
* @noinspection PhpUnused
* @param int $month
* @return Now
* @throws Exception
* @noinspection PhpUnused
*/
public function addMonth(int $month):self
{
$this->dateTime->add(new DateInterval('P' . $month . 'M'));
return $this;
}
/**
* @param int $day
* @return $this
* @throws Exception
*/
public function addDay(int $day): self
{
$this->dateTime->add(new DateInterval('P' . $day . 'D'));
return $this;
}
/**
* @noinspection PhpUnused
* @param int $week
* @return $this
* @throws Exception
*/
public function addWeek(int $week): self
{
return $this->addDay($week * 7);
}
/**
* @noinspection PhpUnused
* @param int $second
* @return $this
* @throws Exception
*/
public function addSecond(int $second): self
{
$this->dateTime->add(new DateInterval('PT' . $second . 'S'));
return $this;
}
/**
* @param int $minute
* @return $this
* @throws Exception
*/
public function addMinute(int $minute): self
{
$this->dateTime->add(new DateInterval('PT' . $minute . 'M'));
return $this;
}
/**
* @param int $hour
* @return $this
* @throws Exception
*/
public function addHour(int $hour): self
{
$this->dateTime->add(new DateInterval('PT' . $hour . 'H'));
return $this;
}
/**
* @return string
*/
public function get(): string
{
return $this->dateTime->format('Y-m-d H:i:s');
}
/**
* @return string
*/
public function __toString()
{
return $this->get();
}
}
/**
* @return Now
* @throws Exception
*/
function now()
{
return new Now();
}
using
echo now(); //2020-03-10 22:10
echo now()->addDay(1); //2020-03-11 22:10
echo now()->addDay(1)->addHour(1); // //2020-03-11 23:10
echo now()->addDay(1)->addHour(1)->addMinute(30); // //2020-03-11 23:40
echo now()->addDay(1)->addHour(1)->addMinute(30)->addSecond(10); // //2020-03-11 23:50
//or u can use get method for example
echo now()->addDay(1)->addHour(1)->addMinute(30)->get(); // //2020-03-11 23:40
Use this function:
function getDatetimeNow() {
$tz_object = new DateTimeZone('Brazil/East');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format('Y\-m\-d\ h:i:s');
}
In PHP the logic equivalent of the MySQL's function now() is time().
But time() return a Unix timestamp that is different from a MySQL DATETIME.
So you must convert the Unix timestamp returned from time() in the MySQL format.
You do it with: date("Y-m-d H:i:s");
But where is time() in the date() function? It's the second parameter: infact you should provide to date() a timestamp as second parameter, but if it is omissed it is defaulted to time().
This is the most complete answer I can imagine.
Greetings.
You might find this helpful
new \DateTime()
date('Y-m-d H:i:s')
Look here for more details: http://pl.php.net/manual/en/function.date.php
Not besides the date function:
date("Y-m-d H:i:s");