V Posted July 10, 2010 Share Posted July 10, 2010 I use DATETIME in my tables for new posts and via php I use INSERT with the now() function to send the current date and time into the DB whenever something new is posted. It works fine but now I'm trying to find out how to edit a post's date from a php admin page I made. The date appears as 2010-07-11 on the site. I'm using this code which was provided to me on the forum $post_date = substr($row['post_date'], 0, -9); .... echo $post_date; How would I change that to month, day, year? for example 07/11/10 Also in the admin page I use a jquery calendar to edit the dates. So 2010-07-11 changes to 07/14/2010 if I choose day 14 but how will I insert that in the DB using the mysql DATETIME format like this? 2010-14-11 11:56:46 Does anyone know please? Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/ Share on other sites More sharing options...
Pikachu2000 Posted July 10, 2010 Share Posted July 10, 2010 Are you sure the calendar doesn't return the date/time from the script in the correct format? Most of them are at least configurable enough to allow you to do that. Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1084243 Share on other sites More sharing options...
V Posted July 10, 2010 Author Share Posted July 10, 2010 Hmm, the calendar returns the correct format but revises my own format. For example from 2010-07-11 to 07/11/2010 But how can I get that format in my site before using the calendar? Also I'm not sure how it would insert 07/11/2010 in the DB table because in there it uses the TIMEDATE format with the time as well and without the 99/99/9999 format Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1084248 Share on other sites More sharing options...
V Posted July 10, 2010 Author Share Posted July 10, 2010 lol really funny youtube vid BTW, the noob reminds me on ME Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1084249 Share on other sites More sharing options...
ChemicalBliss Posted July 11, 2010 Share Posted July 11, 2010 Please use timestamps (PHP's time() or microtime() functions); Will keep consistency of time/dates between php applications, and also much easier to format/manage/manipulate and compare in PHP code. If your fields use Mysql' DATETIME field type you can use the SELECT UNIX_TIMESTAMP(`col`) function to get a tmiestamp from mysql for use in php. This will preserve the ability to use MySQL' Date functions when manipulating data. I personally use timestamps from PHP all the way, this way i know the time is going to be the same, i can predict the data easier through different database since it is an integer created by my script and not some server program (which could very well be in a different timezone). -cb- Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1084265 Share on other sites More sharing options...
myrddinwylt Posted July 11, 2010 Share Posted July 11, 2010 Most date/date&time/time strings can be converted into a PHP timestamp using the following command $stamp = strtotime("YOURSTRINGHERE"); You can then use the formatting commands that PHP has to layout the date/time however you see fit. Date & Time formatting Please note that newer versions of PHP require the use of date_default_timezone_set("America/Toronto"); prior to any date/time string manipulation -- replace "America/Toronto" with whichever your server location is at. See here for a complete list of all available zones selectable by region/country. Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1084294 Share on other sites More sharing options...
V Posted July 11, 2010 Author Share Posted July 11, 2010 Thanks! I changed datetime to timestamp but it outputs the same format. I use this to show the date of posts. db connection and while loop ...... $post_date = $row['post_date']; echo $post_date; It echoes 2010-07-12 12:20:07 when I use the date function like this echo date("m/d/y",time()); it formats 07/12/10. Using timestamp, how can I echo the same for the $post_date var? That's what I don't understand :-\ Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1084481 Share on other sites More sharing options...
PFMaBiSmAd Posted July 11, 2010 Share Posted July 11, 2010 The easiest and fastest way of formating a mysql DATETIME or mysql TIMESTAMP or even a UNIX Timestamp value stored in a database is to do it in the query when you retrieve it. For a mysql DATETIME or mysql TIMESTAMP, use the mysql DATE_FORMAT() function (that's what it is for.) For a UNIX Timestamp, the FROM_UNIXTIME() function accepts a second format parameter of the same style as the DATE_FORMAT() uses. Doing this in a query is at least 8x faster than using some slow parsed/tokenized/interpreted php code. Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1084492 Share on other sites More sharing options...
V Posted July 15, 2010 Author Share Posted July 15, 2010 PFMaBiSmAd thanks for sharing that!! Now it all makes sense Quote Link to comment https://forums.phpfreaks.com/topic/207381-edit-datetime-through-php/#findComment-1086474 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.