Asked  8 Months ago    Answers:  5   Viewed   30 times

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(), but I am asking if there is a function only for this.

For example, to return:

2009-12-01 00:00:00



Not besides the date function:

date("Y-m-d H:i:s");
Wednesday, March 31, 2021
answered 8 Months ago



    $time = time();

    $sql = "INSERT INTO yourtablename (ArrivalTime) Values ('$time')";


P.S: in the sql statement i'm sure you'll need to put other things in the other fields ,so you just replace the one above by this:

$sql = "INSERT INTO yourtablename (field1, field2, ArrivalTime) Values ('$value1','$value2','$time')";



$error = "Couldn't connect";
$connect = mysql_connect("localhost","username","password") or die($error);
mysql_select_db("yourdatabase") or die($error);

Wednesday, March 31, 2021
answered 8 Months ago

You can use the datetime object or their function aliases for this:

Example (abridged from PHP Manual)


$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "n";
$la_time = new DateTimeZone('America/Los_Angeles');
echo $datetime->format('Y-m-d H:i:s');

Edit regarding comments

but i cannt use this method because i need to show date in different time zones as the user login from different locations

That's not a problem. When a user logs in, you determine his timezone and set it to your DateTime object just like shown. I'm using a similar approach in one of my projects and it works like a charm.

in the database i need to get the dates in any single timezone, then only it can be processed properly

You store the time either as a timestamp or a datetime in one timezone. When you query a DateTime field, you either convert the time in a DateTime object to this timezone or - if your db supports it - query with the selected timezone.

Tuesday, June 1, 2021
answered 5 Months ago

I ran into the same issue and found the following solution in the documentation:

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

    <!-- ... -->
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    <!-- ... -->

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
Bálint Molnár
answered 3 Months ago

It works, but the scope of test2() is limited. For example, this works:

[wally@zf ~]$ cat y.php
function test1 ()
        global $x;

        function test2()
                global $x;
                echo $x;


[wally@zf ~]$ php -f y.php
123[wally@zf ~]$
Tuesday, October 26, 2021
answered 2 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :