473,803 Members | 3,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2422
On May 22, 1:51 pm, Michael Sharman <sha...@gmail.c omwrote:
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.blogsp ot.com/)"
<colin.mckin... @gmail.comwrote :
On May 22, 1:51 pm, Michael Sharman <sha...@gmail.c omwrote:
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(MyD ate, '%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.c omwrote:
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...@y ahoo.comwrote:
On 22 May, 13:51, Michael Sharman <sha...@gmail.c omwrote:
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
1787
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 up my script for almost a week now and I still cant get it right. here is the code <?php include ("ES_includes/appconst.inc.php") ;
3
2243
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 records returned when I execute: $sql = "select * from tbl where ExpiryDate > " . date("Y-m-d"); mysql_query($sql);
20
2641
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 functions like DATE() or NOW(), I don't get a Unix value, (but rather a formatted date). So I cannot use those functions to check against my saved value.
5
1526
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) ORDER BY
5
3497
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, because the dates must have quotes on each side. I just don't know how make the dates right. Well I'll just show you the code and some insert statements it generates. Could anyone please help me?
2
5045
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 me how to extract all data from the start of the
6
38948
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 Unix-style timestamp and then formats it. $timestamp = "2005-04-06 15:43:34"; $time = strtotime($timestamp); print date('Y-m-d \a\t H:i', $time)."\n"; However, it seems kind of counter productive. After all, aren't people
5
3666
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 (variable dates) lie within certain ranges of times given to calcutate pricing for a hotel which has different prices depending on the season. (fixed dates). So I have this table (MYSQL) which for example says: from : to : price/night 01/01 :...
8
3060
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
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.