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
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
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);
?>
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.
Just so you're aware, if you're going to have the php pages you're requesting also request other pages themselves, it may be beneficial to use require_once
instead of include
. That will make it so none of the pages that are included repeat themselves and you don't have to worry about accidentally including something more than once.
That being said... when you request a page in the root directory, it will request header.php in the root directory which will in turn request functions.php in the root directory. However, if you request from the subdirectory, ../header.php
will reference header.php in the root directory, but that whole file will get included, and then its the php page in the subdirectory that ends up trying to include /functions.php
. It would need to request ../functions.php
, but that would cause everything in the root directory to stop working.
I'd suggest setting a variable in header.php along the lines of $root = $_SERVER['DOCUMENT_ROOT'];
Then, all includes in header.php should be like include($root."/functions.php");
$_SERVER['DOCUMENT_ROOT']
will get you objective url to the root, which will enable you to make sure you're referencing the correct place no matter where you're requesting header.php from.
It works, but the scope of test2()
is limited. For example, this works:
[[email protected] ~]$ cat y.php
<?php
function test1 ()
{
global $x;
$x=123;
function test2()
{
global $x;
echo $x;
}
test2();
}
test1();
?>
[[email protected] ~]$ php -f y.php
123[[email protected] ~]$
Not besides the date function: