PHP_CHILD Posted January 22, 2013 Share Posted January 22, 2013 (edited) 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 January 22, 2013 by PHP_CHILD Quote Link to comment https://forums.phpfreaks.com/topic/273478-php-beginners-basic-simple-method-to-store-date-in-my-myphpadmin/ Share on other sites More sharing options...
Christian F. Posted January 22, 2013 Share Posted January 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273478-php-beginners-basic-simple-method-to-store-date-in-my-myphpadmin/#findComment-1407445 Share on other sites More sharing options...
Barand Posted January 22, 2013 Share Posted January 22, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273478-php-beginners-basic-simple-method-to-store-date-in-my-myphpadmin/#findComment-1407447 Share on other sites More sharing options...
PHP_CHILD Posted January 23, 2013 Author Share Posted January 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273478-php-beginners-basic-simple-method-to-store-date-in-my-myphpadmin/#findComment-1407639 Share on other sites More sharing options...
PHP_CHILD Posted January 23, 2013 Author Share Posted January 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273478-php-beginners-basic-simple-method-to-store-date-in-my-myphpadmin/#findComment-1407641 Share on other sites More sharing options...
Barand Posted January 23, 2013 Share Posted January 23, 2013 ... $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 Quote Link to comment https://forums.phpfreaks.com/topic/273478-php-beginners-basic-simple-method-to-store-date-in-my-myphpadmin/#findComment-1407661 Share on other sites More sharing options...
PHP_CHILD Posted January 23, 2013 Author Share Posted January 23, 2013 ... $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 Quote Link to comment https://forums.phpfreaks.com/topic/273478-php-beginners-basic-simple-method-to-store-date-in-my-myphpadmin/#findComment-1407707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.