NOW() function in PHP

前端 未结 20 1469
遥遥无期
遥遥无期 2020-11-28 17:26

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

相关标签:
20条回答
  • 2020-11-28 17:58

    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
    
    0 讨论(0)
  • 2020-11-28 17:59

    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');
    }
    
    0 讨论(0)
  • 2020-11-28 18:00

    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.

    0 讨论(0)
  • 2020-11-28 18:02

    You might find this helpful

    new \DateTime()
    
    0 讨论(0)
  • 2020-11-28 18:03
    date('Y-m-d H:i:s')
    

    Look here for more details: http://pl.php.net/manual/en/function.date.php

    0 讨论(0)
  • 2020-11-28 18:07

    Not besides the date function:

    date("Y-m-d H:i:s");
    
    0 讨论(0)
提交回复
热议问题