Jump to content

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....

Edited by PHP_CHILD

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.