Jump to content

Recommended Posts

Hi,

 

I am creating a website using PHP and MYSQL for my dissertation. The main element of the site is an online application form. When each application is submitted, the date and time is recorded using a timestamp.

 

I was wondering if there is a way I can use this to produce a query showing all the applications submitted today? I wanted to have an edit box so the user can enter today's date and this then searches the submitted field and compares this to the TIMESTAMP. There is only one problem both the date and time are recorded, I wondered if there is a way to search just the date and not the time of the TIMESTAMP?

 

If this is not possible (or really complicated) is there an easier way to create this query?

 

Any help would be great... this has been driving me crazy!

 

Pam

Link to comment
https://forums.phpfreaks.com/topic/97347-timestamp-query/
Share on other sites

Will the date need to be converted to SQL format? This is probably obvious but I'm not sure how to enter the CURDATE() value. I am using Dreamweaver, so should I use a hidden field and assign the value to this field?

 

 

I have been having trouble with dates as well. Is there a simple way to convert a date input as dd/mm/yyyy to the MySQL format?

 

 

Pam

Link to comment
https://forums.phpfreaks.com/topic/97347-timestamp-query/#findComment-498126
Share on other sites

I'm not sure how to enter the CURDATE() value.

 

INSERT INTO mytable (datecol) VALUES ( CURDATE() )

 

 

I have been having trouble with dates as well. Is there a simple way to convert a date input as dd/mm/yyyy to the MySQL format?

 

if $dmy = '22/03/2008'

 

INSERT INTO mytable (datecol) VALUES ( STR_TO_DATE( '$dmy' , '%d/%m/%Y') )

 

Link to comment
https://forums.phpfreaks.com/topic/97347-timestamp-query/#findComment-498203
Share on other sites

this is what I use in my site to figure out the difference in days between a made time and the current time

 

$century = mktime(12, 0, 0, 3, 13, 2008);
$today = time();
$timedifference = $today - $century;
$moddifference = floor($timedifference / 86400);

 

if you replace the $century variable with the time you get from an sql field (one that comes with the post). Then you can get the time that corresponds with the post.

 

I divide by 86400 for a reason. There are 60 seconds in a minute, 60 minutes in an hour and 24 hours in a day. So 60*60*24 = 86400

 

So there are 86400 seconds in a day. The $timedifference is the difference in seconds between the two dates, so if you divide it by 86400 you get it in days.

 

Naturally this is done for only one post. Now all you have to do is figure out a way, to get all the posts you want to check it for and then use an if-else construction to check wether it's a day old or not (or the way you want it to, like 2 days or whatever).

 

I'm only a noob at this, but I hope this helps you. Best of luck!

Link to comment
https://forums.phpfreaks.com/topic/97347-timestamp-query/#findComment-498240
Share on other sites

SELECT TIMEDIFF(`appointment_start`,`appointment_end`) AS duration FROM appointments;

 

or

 

SELECT IF(TIME_TO_SEC(last_date)>=TIME_TO_SEC(first_date),
TIME_TO_SEC(last_date)-TIME_TO_SEC(first_date),
86400+(TIME_TO_SEC(last_date)-TIME_TO_SEC(first_date)))
FROM table;

Link to comment
https://forums.phpfreaks.com/topic/97347-timestamp-query/#findComment-498245
Share on other sites

If the value of the date in this insert statement comes from an edit box, can anyone point out where I'm going wrong:

 

 $insertSQL = sprintf("INSERT INTO project (Project_Title, Start_Date, End_Date) 
VALUES %s, (STR_TO_DATE(%s, '%d/%m/%y'), STR_TO_DATE(%s, '%d/%m/%y')",
                       GetSQLValueString($_POST['Project_Title'], "text"),
                       GetSQLValueString($_POST['Start_Date'], "date"),
                       GetSQLValueString($_POST['End_Date'], "date") .... 

 

I'm pretty new to all this...

 

Pam

Link to comment
https://forums.phpfreaks.com/topic/97347-timestamp-query/#findComment-498886
Share on other sites

This is the complete statement I just edited it a bit before:

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "application_form")) {
  $insertSQL = sprintf("INSERT INTO project (Project_name, Project_code, Project_qc_name, Start_Date, End_Date, BEN_Time, BEN_Distributer_email, Out_of_hours_support, Details, Size_id, ClearQuest_id, Platform_id, Project_manager_id, Testing_leader_id, Comments, submitted, `Time`) VALUES (%s, %s, %s, STR_TO_DATE(%s, '%d/%m/%y'), STR_TO_DATE(%s, '%d/%m/%y'), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, curdate(), curtime())",
                       GetSQLValueString($_POST['Project_Title'], "text"),
                       GetSQLValueString($_POST['Project_Code2'], "text"),
                       GetSQLValueString($_POST['Project_QC_Title'], "text"),
                       GetSQLValueString($_POST['Start_Date'], "date"),
                       GetSQLValueString($_POST['End_Date'], "date"),
                       GetSQLValueString($_POST['BEN_Time'], "double"),
                       GetSQLValueString($_POST['BEN_Distributer_Email'], "text"),
                       GetSQLValueString($_POST['outofhours'], "text"),
                       GetSQLValueString($_POST['Details'], "text"),
                       GetSQLValueString($_POST['Size'], "int"),
                       GetSQLValueString($_POST['ClearQuest_Project'], "int"),
                       GetSQLValueString($_POST['Platform'], "int"),
                       GetSQLValueString($_POST['Project_Manager'], "int"),
                       GetSQLValueString($_POST['Testing_Leader'], "int"),
                       GetSQLValueString($_POST['Comments'], "text"),
                       GetSQLValueString($_POST['curdate'], "date"),
                       GetSQLValueString($_POST['hiddenField'], "date"));

  mysql_select_db($database_Project_Application, $Project_Application);
  $Result1 = mysql_query($insertSQL, $Project_Application) or die(mysql_error());

 

At the moment if I run the code I get this error:

 

Warning: sprintf() [function.sprintf]: Too few arguments in C:\wamp\www\Project_Application\projectapplication.php on line 55
Query was empty

Link to comment
https://forums.phpfreaks.com/topic/97347-timestamp-query/#findComment-498987
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.