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

 Answers

18

Not besides the date function:

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

main.php

<?php
    require('connect.php');

    $time = time();


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

    ?>

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')";

connect.php

<?php

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

?>
Wednesday, March 31, 2021
 
toesslab
answered 8 Months ago
51

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

Example (abridged from PHP Manual)

date_default_timezone_set('Europe/London');

$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');
$datetime->setTimezone($la_time);
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
 
eek
answered 5 Months ago
eek
59

I ran into the same issue and found the following solution in the documentation: http://symfony.com/doc/current/book/testing.html#your-first-functional-test

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:

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

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
37

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

[wally@zf ~]$ cat y.php
<?php
function test1 ()
{
        global $x;
        $x=123;

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

        test2();
}

test1();
?>
[wally@zf ~]$ php -f y.php
123[wally@zf ~]$
Tuesday, October 26, 2021
 
Jimmy
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 :
 
Share