473,399 Members | 3,038 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,399 software developers and data experts.

php/mysql date

hi,
I have some problems related to date type:
In mysql, I created a table with some fields of date type, so how can I use php to insert date value in into database (then db will store date format, instead of breaking it into separate smaller attributes: date, month, year...with int type like i did before- I don't like it)

Then when I call out these value, how can I perform calculation (in php, of course). For example: how many days from 13/3/08 to 31/4/08 or from 20/5/2008 to 10/6/2008

thanks
May 6 '08 #1
4 2154
rpnew
188 100+
hi,
I have some problems related to date type:
In mysql, I created a table with some fields of date type, so how can I use php to insert date value in into database (then db will store date format, instead of breaking it into separate smaller attributes: date, month, year...with int type like i did before- I don't like it)

Then when I call out these value, how can I perform calculation (in php, of course). For example: how many days from 13/3/08 to 31/4/08 or from 20/5/2008 to 10/6/2008

thanks
Hi,
There is datatype date and datetime in MySql in which you can store your date and date with timestamp respectively. So you can define that column in database and using date function in PHP you can insert it into database.

Not much sure about calculation regarding dates...

Regards,
RP
May 6 '08 #2
ronverdonk
4,258 Expert 4TB
When possible try to calculate the date and time differences in your MySQL select, there are many functions in MySQL that allow that.

Is it really necessary to calculate these values in PHP, i.e. outside MySQL?

Ronald
May 6 '08 #3
I've found way to insert and print out the date-type data in 'date-month-year' (insead of 'Year-month-date' form in mysql) by using strtotime()function, however still dont know how to subtract two dates.

Hmm, actually I may not care whether it's done inside or outside mySQL. Given a case that I insert job information into database, which includes add_date and expire_date. so I need to calculate the substraction of the two date, then when the result is equal or greater than 0 (i.e the job is expired) then the job status will change from available to expired. If this can hardly be done in php so how to do it in form of sql query then using php's mysql_query() to run it.

The 2nd case is that when a user logs out of a system, that moment will be stored in db as last login time, and now I want to query all the jobs which are added from the last login time until the moment the user logs in again (i.e all new added jobs). Can it be like "SELECT * FROM job_table WHERE add_date > lastlogin AND add_date <= $now" or else?
May 9 '08 #4
rpnew
188 100+
I've found way to insert and print out the date-type data in 'date-month-year' (insead of 'Year-month-date' form in mysql) by using strtotime()function, however still dont know how to subtract two dates.

Hmm, actually I may not care whether it's done inside or outside mySQL. Given a case that I insert job information into database, which includes add_date and expire_date. so I need to calculate the substraction of the two date, then when the result is equal or greater than 0 (i.e the job is expired) then the job status will change from available to expired. If this can hardly be done in php so how to do it in form of sql query then using php's mysql_query() to run it.

The 2nd case is that when a user logs out of a system, that moment will be stored in db as last login time, and now I want to query all the jobs which are added from the last login time until the moment the user logs in again (i.e all new added jobs). Can it be like "SELECT * FROM job_table WHERE add_date > lastlogin AND add_date <= $now" or else?
Hi,
You can use following...
Expand|Select|Wrap|Line Numbers
  1. select * from table where date between start_date and end_date;
  2.  
Some formating might be needed with date.. not sure though..

Regards,
RP
May 9 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Neil | last post by:
Hi, I hope this question isn't too far off topic....I'm almost at my wits end trying to figure this out. I have a Mysql database and I wish to automate the backup of the database because I...
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...
4
by: RT | last post by:
If anyone can help that would be great. Iım trying to format a timestamp from my MySQL table (sessions) Hereıs the code Iım using: <?php echo date('D,n-j-y h:i:s...
0
by: David Bordas | last post by:
Hi list, I've got a little bug with MySQL. I can insert a row into my table but this row will not appear in the table :( Server is under linux redhat, MySQL is 3.23.56 installed from binary...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
1
by: windandwaves | last post by:
Hi Folk Some of you may be interested in a function that allows you to add/substract from a date in PHP, using a MySql Dateformat (e.g. 2005-10-31) (NOT TESTED): Any comments appreciated. ...
4
by: Dan Lewis | last post by:
I've imported a ms access database into a table in a mysql database. The access database contains a field that holds date/time values in 'general date' format. These all show up at 01/01/1970 in...
12
by: mantrid | last post by:
Hello Can anyone point me in the right direction for the way to read a text file a line at a time and separate the fields on that line and use them as data in an INSERT to add a record to a mysql...
1
by: paulq182 | last post by:
PLEASE HELP ME WITH MY CODE?? import java.sql.*; import java.io.*; class min_filmdb_rel_mysql { public static void main (String args ) throws SQLException, IOException {
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.