PHP_CHILD Posted January 22, 2013 Share Posted January 22, 2013 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.... 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. 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 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. 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 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 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 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
Archived
This topic is now archived and is closed to further replies.