473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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()?

Jun 2 '08 #1
4 2403
On May 22, 1:51 pm, Michael Sharman <sha...@gmail.comwrote:
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.
Jun 2 '08 #2
On May 22, 11:02 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
On May 22, 1:51 pm, Michael Sharman <sha...@gmail.comwrote:
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.
Jun 2 '08 #3
On 22 May, 13:51, Michael Sharman <sha...@gmail.comwrote:
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."
Jun 2 '08 #4
On May 23, 2:33 am, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 22 May, 13:51, Michael Sharman <sha...@gmail.comwrote:
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
Jun 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tiernan | last post by:
Hey everybody. I'm verrynew to PHP and MYSQL and have been working on a form that when it is submitted stores the information into a mysql database. The main problem is that i'm trying to finish...
3
by: Mark Stevens | last post by:
I am trying to do select all of the records in a table where they data has not expired. The table contains two fields Subject (varchar(100)) and ExpiryDate (Date). I am getting all of the...
20
by: Sims | last post by:
Hi, I have a field in my DB that saves the date/time as a integer. I get the time on my server using the time() function. now, moving away from php and looking at (My)SQL only. If I use...
5
by: Bob | last post by:
This little date formating problem is holding me up! Here is the sql query string. ..... WHERE (`work_orders`.`date_closed` > $ > $start_date) AND (`work_orders`.`date_closed` < $end_date)...
5
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql,...
2
by: toedipper | last post by:
Hello, MYsql and PHP If I want to extract data with todays date then it's 'where blab blah = current_date()' For yesterday it's 'where blah blah = current_date()-1' But can anyone tell...
6
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a...
5
by: dennisfreud | last post by:
Hi, I am looking for general help here. I dont know how to start at this... maybe someone can point me in the right direction. I need to calculate how many nights of a given time frame...
8
by: Tony B | last post by:
I have a string in a existing php script which is in the form "dd/mm/yyyy" and I need to convert it into a suitable format for mysql which is "yyyy-mm-dd" Is there a neat way of doing this in php ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.