Jump to content

php beginners-basic simple method to store date in my myphpadmin


PHP_CHILD

Recommended Posts

Hello all, i googled for hours to get the date store in my db. I know i must keep the datatype as either date or datetime in coulmn. And i also got to know that i must use STR_TO_DATE to store values. i just want to have a coulmn storing date and time(Indian time) just to get the last 5 entries of my table.

i tried this

 

 

$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$phpdate=$date->setTimezone($timezone );

$query_autonow = "INSERT INTO customers (dateadded)
VALUE (STR_TO_DATE('$phpdate'))";
mysql_query($query_autonow) or die(mysql_error());

 

 

 

i tried various options but i get 0000-00-00 00:00:00.. or i get Fatal error: Class 'Date' not found in /home/update.php on line 6

[size=4]$date = new Date();


$query_autonow = "INSERT INTO customers (dateadded)
VALUE (STR_TO_DATE('$date'))";
$hh=mysql_query($query_autonow) or die(mysql_error());[/size]

 

 

when i use this....

if anybody have time, pls guide.. Many thanks in advances....

If you're going to store the current date/time of when the record is inserted, why not just use CURRENT_TIMESTAMP in MySQL? No need to involve PHP for this. ;)

thanks....

so if i need to use find the last login time for all users.....what should i do.

You only need STR_TO_DATE if the format is not yyyy-mm-dd. Also STR_TO_DATE needs a format parameter to define the current format

<?php include('connect.php'); 
$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$phpdate=$date->setTimezone($timezone);
$query_autonow = "INSERT INTO customers (dateadded)
VALUE ('$phpdate')";
mysql_query($query_autonow) or die(mysql_error())
?>

and am getting only 0000 values as date....pls help

...
$phpdate=$date->setTimezone($timezone);
$strdate = $date->format('Y-m-d');                   // create date string in required format
$query_autonow = "INSERT INTO customers (dateadded)
VALUE ('$strdate')";                                 // use that string in the query  

...
$phpdate=$date->setTimezone($timezone);
$strdate = $date->format('Y-m-d'); // create date string in required format
$query_autonow = "INSERT INTO customers (dateadded)
VALUE ('$strdate')"; // use that string in the query

thanks a lot :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.