Jump to content

edit DATETIME through php


V

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.