PHP + MySQL + Dates = ?

This is a discussion on PHP + MySQL + Dates = ? within the PHP Language forums, part of the PHP Programming Forums category; Hi guys, I'm a little confused with dates. Ok, all I want to do is store a date in ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-22-2008
Michael Sharman
 
Posts: n/a
Default PHP + MySQL + Dates = ?

Hi guys,

I'm a little confused with dates.

Ok, all I want to do is store a date in MySQL as a datetime object and
be able to read and format it using PHP. My datatype in MySQL is
DATETIME and I'm inserting a date using the MySQL function now(). So
far so good.

When I var_dump() the value from the database it says it's a string,
so I can't use date_format() on it.

What do people mainly do here? I know I can use:

date('Y-M-d H:i:s', strtotime($row["spotdate"]);

But my question is how do I store a datetime object in MySQL so I can
use PHP functions like date_format()?

Reply With Quote
  #2 (permalink)  
Old 05-22-2008
C. (http://symcbean.blogspot.com/)
 
Posts: n/a
Default Re: PHP + MySQL + Dates = ?

On May 22, 1:51 pm, Michael Sharman <sha...@gmail.com> wrote:
> Hi guys,
>
> I'm a little confused with dates.
>
> Ok, all I want to do is store a date in MySQL as a datetime object and
> be able to read and format it using PHP. My datatype in MySQL is
> DATETIME and I'm inserting a date using the MySQL function now(). So
> far so good.
>
> When I var_dump() the value from the database it says it's a string,


Yes - PHP's internal time representation is based on Unix timestamps -
there's a function in MySQL to return a Unix timestamp. MySQL formats
dates on output - without the complex mapping implemented by something
like PHP's date(), string is the nearest safe base type.

> so I can't use date_format() on it.
>
> What do people mainly do here? I know I can use:
>
> date('Y-M-d H:i:s', strtotime($row["spotdate"]);
>
> But my question is how do I store a datetime object in MySQL so I can
> use PHP functions like date_format()?


Don't - do your formatting/date calculations in the database - MySQL
will happily handle times from 01 Jan 0 AD (but probably not valid
before the last major calendar changes) to 31 Dev 9999. Unix time only
runs from midnight, 1 Jan 1970 to 03:14:07, January 19, 2038 -less
than a single lifetime.

C.
Reply With Quote
  #3 (permalink)  
Old 05-22-2008
Michael Sharman
 
Posts: n/a
Default Re: PHP + MySQL + Dates = ?

On May 22, 11:02 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.com> wrote:
> On May 22, 1:51 pm, Michael Sharman <sha...@gmail.com> wrote:
>
> > Hi guys,

>
> > I'm a little confused with dates.

>
> > Ok, all I want to do is store a date in MySQL as a datetime object and
> > be able to read and format it using PHP. My datatype in MySQL is
> > DATETIME and I'm inserting a date using the MySQL function now(). So
> > far so good.

>
> > When I var_dump() the value from the database it says it's a string,

>
> Yes - PHP's internal time representation is based on Unix timestamps -
> there's a function in MySQL to return a Unix timestamp. MySQL formats
> dates on output - without the complex mapping implemented by something
> like PHP's date(), string is the nearest safe base type.
>
> > so I can't use date_format() on it.

>
> > What do people mainly do here? I know I can use:

>
> > date('Y-M-d H:i:s', strtotime($row["spotdate"]);

>
> > But my question is how do I store a datetime object in MySQL so I can
> > use PHP functions like date_format()?

>
> Don't - do your formatting/date calculations in the database - MySQL
> will happily handle times from 01 Jan 0 AD (but probably not valid
> before the last major calendar changes) to 31 Dev 9999. Unix time only
> runs from midnight, 1 Jan 1970 to 03:14:07, January 19, 2038 -less
> than a single lifetime.
>
> C.


Thanks for that, working well now.

In MySQL I'm using:

date_format(MyDate, '%W %D %b')

Now in my PHP I simply have to output $results["MyDate"];

Thanks again.
Reply With Quote
  #4 (permalink)  
Old 05-22-2008
Captain Paralytic
 
Posts: n/a
Default Re: PHP + MySQL + Dates = ?

On 22 May, 13:51, Michael Sharman <sha...@gmail.com> wrote:
> Hi guys,
>
> I'm a little confused with dates.
>
> Ok, all I want to do is store a date in MySQL as a datetime object and
> be able to read and format it using PHP. My datatype in MySQL is
> DATETIME and I'm inserting a date using the MySQL function now(). So
> far so good.
>
> When I var_dump() the value from the database it says it's a string,
> so I can't use date_format() on it.
>
> What do people mainly do here? I know I can use:
>
> date('Y-M-d H:i:s', strtotime($row["spotdate"]);
>
> But my question is how do I store a datetime object in MySQL so I can
> use PHP functions like date_format()?


If all you want is to store a date, you should be using the DATE
datatype, see:
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
where it says:
"The DATE type is used when you need only a date value, without a time
part."
Reply With Quote
  #5 (permalink)  
Old 05-22-2008
Michael Sharman
 
Posts: n/a
Default Re: PHP + MySQL + Dates = ?

On May 23, 2:33 am, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 22 May, 13:51, Michael Sharman <sha...@gmail.com> wrote:
>
>
>
> > Hi guys,

>
> > I'm a little confused with dates.

>
> > Ok, all I want to do is store a date in MySQL as a datetime object and
> > be able to read and format it using PHP. My datatype in MySQL is
> > DATETIME and I'm inserting a date using the MySQL function now(). So
> > far so good.

>
> > When I var_dump() the value from the database it says it's a string,
> > so I can't use date_format() on it.

>
> > What do people mainly do here? I know I can use:

>
> > date('Y-M-d H:i:s', strtotime($row["spotdate"]);

>
> > But my question is how do I store a datetime object in MySQL so I can
> > use PHP functions like date_format()?

>
> If all you want is to store a date, you should be using the DATE
> datatype, see:http://dev.mysql.com/doc/refman/5.0/en/datetime.html
> where it says:
> "The DATE type is used when you need only a date value, without a time
> part."


Sorry, I wasn't clear in my original post. I actually want a datetime,
was just having problems with the formating as PHP treats dates as
timestamps rather than odbc datetime objects etc
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:37 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0