I'm getting date and time by HTTP GET and trying to write a php code to insert these to MYSQL db but getting error.
My date and time are coming like this:
date=260117
time=180205.000
$year= "20" . substr($date, 4,2);
$month= substr($date, 2,2);
$day= substr($month, 0,2);
$dateformatted= $year . "-" . $month. "-" . $day;
$hour= substr($time, 0,2) + 3;
$min = substr($time, 2,2);
$sec = substr($time, 4,2);
$timeformatted = $hour . ":" . $min . ":" . $sec;
$datetime = $dateformatted . " " . $timeformatted;
$datetime = strtotime($datetime);
My error is like that:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '21:02:05)' at line 2
SQL:
$sql = "INSERT INTO pt_position (date) VALUES ($datetime)";
Remove this line.
$datetime = strtotime($datetime);
If you run this line, the return value is PHP's DateTime type.
$datetime = $dateformatted . " " . $timeformatted;
After this code, you can run the sql query....
Related
I am trying to export a certain number of days and setting it to the variable $max_future. Currently it is a set amount of days but I want the users input variable to be the number it has instead.
Currently it is set as:
$max_future = date("Y-m-d", strtotime($today . "+6 days"));
I want something like:
$exportDays = '9'; //(or whatever the user input was)
$max_future = date("Y-m-d", strtotime($today . "+$exportDays days"));
Is this possible? I appreciate any help
Try:
$max_future = date("Y-m-d", strtotime($today . "+" . $exportDays . " days"));
Something like this :
<?php
$output = 9; // you can take this value from input.
$today = date("Y-m-d");
echo date('Y-m-d', strtotime($today. ' +'.$output. ' days'));
?>
<?php
$exportDays = $_REQUEST['exportDays']; // or however you want to get it from user input
$max_date = date('Y-m-d', strtotime(date('Y-m-d') . ' +' . $exportDays . ' days'));
This question already has answers here:
mysql select records greater than 3 months
(3 answers)
Closed 9 years ago.
What I'm trying to do here is create a sql statement to go after some WHERE conditions, this statement is $build. What I'm attempting to do here is take the integer $age which is a value in months, and then I want the statement to make it so that only results older than $age months are returned in my sql statement. The column datetime contains the age of the row in the table, screenshot of the column
$sql="SELECT * FROM $tbl_name WHERE type='plugin' and access_restriction<'$permissions'";
if ($age!=""){
$time = date('H:i:s', time());
$years = date('Y');
$months = date('m');
$days = date('d');
$newmonths = $months - $age;
while ($newmonths <=0) {
$years = $years - 1;
$newmonths = $newmonths +12;
}
$age = $years . "/" . $months . "/" . $days . " " . $time;
$build = $build . " and datetime<CAST('" . $age . "' AS DATE)";
}
$sql = $sql . $build;
I included $sql at the beginning and $sql . $build at the end so you can see how they fit together
Thanks for any help, If I've worded something badly or you don't understand please comment.
Note, if $age = 0; initially it returns the values
If you want to have the date 5 months in the past you can do this:
$howManyMonthsBack = 5;
$dateAgo = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('m') - $howManyMonthsBack , date('d'), date('Y')));
and then in mysql you should use datediff(date1, date2) to compare dates:
WHERE datediff(DATE_COLUMN, $dateAgo) >= 1
or you could use something like this:
WHERE DATE_COLUMN <= NOW() - INTERVAL 5 MONTHS
Could do with some help. So I've got a site which can only be access between two dates. In my database I've got the following
Name Type Data
start_date VARCHAR(255) - 11/01/2012
start_time VARCHAR(255) - 14:32
end_date VARCHAR(255) - 13/01/2012
end_time VARCHAR(255) - 17:20
I need to only allow access to users only on the above dates. Here's the code I'm using, but it doesn't seem to work am I doing something wrong? I'm running PHP4
$current_date = strtotime(date("Y-m-d H:i:s"));
//db query and while loop here
$start_date = explode("/", $rows['start_date']);
$start_time = $rows['start_time'];
$start_date = strtotime($start_date[2] . "-" . $start_date[1] . "-" . $start_date[0] . " " . $start_time);
$end_date = explode("/", $rows['end_date']);
$end_time = $rows['end_time'];
$end_date = strtotime($end_date[2] . "-" . $end_date[1] . "-" . $end_date[0] . " " . $end_time);
if(($current_date <= $start_date) || (($current_date >= $end_date)))
{
echo "Site can only be access between" . $start_date . " and " . $end_date;
}
else
{
// redirect to the site...
}
Thanks in advance :)
I'm debugging a conference room booking calendar which was put together by someone which is no longer working with me. It's been a nightmare since there were so many things wrong with it, but I'm having trouble figuring out exactly what's causing this last bug. The calendar checks to see if rooms have already been booked at certain times and they aren't having an issue showing as booked at the right time, but if someone tries to book the same room an hour or less after the room is vacated after a shift to or from DST, it shows the room as booked still.
Example:
User sees that a room is reserved for Nov. 29th from 9am-10am.
User then tries to book the room on Nov. 29th from 10:30am-12:00pm.
The calendar cancels this second request and informs the user that the room is already booked.
It should be noted that this doesn't happen any time before the shift to DST (Nov. 4th). Here's the function which determines if the room is available:
function calCheck($starttime, $endtime, $cal_name, $cat_id, $myDB, $myHost, $myUser, $myPass) {
$timezone = 'America/Denver';
date_default_timezone_set ($timezone);
$dset = new DateTime($odate, new DateTimeZone($timezone));
$dset2 = $dset->getOffset();
//$starttime = $starttime + 1;
//$endtime = $endtime - 1;
$starttime = $starttime - $dset2 + 1;
$endtime = $endtime - $dset2 - 1;
$starttime = $starttime;
$endtime = $endtime;
//echo $starttime .'</br>'. $endtime . '</br>';
$db = new myDB($myDB, $myHost, $myUser, $myPass);
$db->myDB_Connect();
//echo 'calcheck</br>';
$ck_query = 'SELECT * FROM vw_cal_chk
WHERE (stime < '. $starttime . ' AND etime > ' . $starttime . ' ) and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
OR (stime < ' . $endtime . ' AND etime > ' . $endtime . ') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"
OR (stime >= '. $starttime . ' AND etime < ' . $endtime .') and Calendar = "' . $cal_name . '" and cat_id = "' .$cat_id . '"';
$ck_result = $db->myQuery($ck_query);
$num = mysql_num_rows($ck_result);
//echo $ck_query . '</br>' . $num;
if ($num >> 0){
$avail = 1;
} else {
$avail = 0;
}
return $avail;
}
All of my timestamps up to this point are in UTC and I notice the $odate variable is actually never instantiated anywhere, but I haven't been able to determine what value to pass it in order for the offsetting to work correctly. If I can find out what kind of date it wants, I should be able to work the rest out.
$odate is null which defaults to now giving you the current offset. What you want is the offset at the time that the schedule is being set not the offset right at this moment. It looks like your database dates are in Mountain time and that your startdate is utc and you are subtracting the offset to get back to mountain (I would think that you would add the offsets not subtract , so I might have this reversed, can you query your database and figure it out?)
In any event you should be computing your offset using $startdate not now(), try switching $startdate for $odate and see what happens. If this doesn't work try building a string from startdate and then generating a new date from that string. If nothing else outputting that string should give you a clear picture as to whether $startdate is UTC or mountain
I have the following types of strings:
17082011T1015
In other words: day month year T hours minutes.
What would be an efficient way to parse these strings in such a way that I end with something like a MySQL-like date:
`2011-08-17 10:15`
(There's no MySQL involved here by the way)
It's for Windows PHP < 5.3 so strptime() and date_parse_from_format()/DateTime::createFromFormat() are NOT an option.
It's just rearranging a string.
$date = "17082011T1015";
$day = substr($date, 0, 2);
$month = substr($date, 2, 2);
$year = substr($date, 4, 4);
$hour = substr($date, 9, 2);
$minute = substr($date, 11, 2);
echo $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute;
I think I'll end up using this solution:
sscanf( '17082011T1015', '%02s%02s%04sT%02s%02s', $day, $month, $year, $hours, $minutes );
$datetime = $year . '-' . $month . '-' . $day . ' ' . $hours . ':' . $minutes;
Before asking the question I didn't realize I could utilize the optional arguments to sscanf() to my advantage, and thought I would have to resort to something like this:
$parsed = sscanf( '17082011T1015', '%02s%02s%04sT%02s%02s' );
$datetime = $parsed[ 2 ] . '-' . $parsed[ 1 ] . '-' . $parsed[ 0 ] . ' ' . $parsed[ 3 ] . ':' . $parsed[ 4 ];
Not too bad either, but I like to be pretty clear as possible with named variables, so that it's obvious what this little routine does.
If you are using PHP >= 5.3.0, you may use DateTime::createFromTimeFormat to create ordinary DateTime object from that string.
Should be something like:
$str = "17082011T1015";
$date = DateTime::createFromFormat($str,'DDMMYYYYThhII');
echo $date->format("Y-m-d H:i:s");
Can't test it here, only have PHP 5.2. But I think you could figure the right format yourself.