I have the following variables...
$pickupDate = $row['pickupDate'];
$pickupTime = $row['pickupTime'];
$dateRetrieve = date("y/m/d");
$currentDate = "20".$dateRetrieve;
$currentTime = date("h:i:sa")
I have an SQL table providing the Pickup Date and Time. I would like to compare the current date/time with the ones in the table, and list all the entities within 2 hours of the current date/time.
Thanks for your help!
You Can Use As Following
$currenttime = date('Y-m-d H:i:s');
//DISPLAY CURRENT TIME
$lasttwohour = date('Y-m-d H:i:s', strtotime('-2 hour'));
//TWO HOUR AGO TIME
$query = "select * from tbl_nm where '$pickupTime' between '$currenttime' AND '$lasttwohour' order by id ";
Or if you dont have datetime field you can remove ('Y-m-d') from $currenttime and $lasttwohour.
I Hope it might helpful.
Related
I have a table transaction, which have created date in UTC, I want to get all the data with New York Timezone in mysql, can anyone please help me for that?
here is my query for that
$user_db = Common_Users_Helper::get_user_db_conn($this->session->userdata('id'), '');
$user_db->select('amount, created,MONTHNAME(created) as month, sponsorion_fees, processor_fees, amount_after_fees, SUM(amount_after_fees) as total');
$user_db->from('transaction');
$user_db->order_by('id', 'ASC');
$user_db->group_by('month');
$user_db->where('record_owner_user_id', $id);
$user_db->where('is_one_time_purchase', 'Y');
$user_db->where('created <= utc_timestamp() and created >= Date_add(utc_timestamp(),interval - 12 month)');
$query = $user_db->get();
//echo $user_db->last_query(); die;
$result = $query->result_array();
You can get current time in New York by setting timezone in PHP as,
date_default_timezone_set('America/New_York');
$currentdatetime = date('Y-m-d H:i:s');
Then in your query, replace utc_timestamp() with $currentdatetime
$user_db->where('created <= "'.$currentdatetime.'" and created >= Date_add("'.$currentdatetime.'",interval - 12 month)');
To display created time in New York timezone,
// Set based timezone as UTC
date_default_timezone_set('UTC');
$date = '2018-11-15 10:56:00';
$timestamp = strtotime($date); // create timestamp
date_default_timezone_set('America/New_York'); // Set required timezone
echo $nydate = date('Y-m-d H:i:s', $timestamp); // Convert timestamp in NY
I am trying to add 2 hours to $server_time and call it $hourPlusTwo. Everything i have tried ends up as either something like 7200 or some obscure date from 1969. How would you do it with what i have here, or rewritten a completely different way? Please understand i am new to php and programming in general. I am trying to understand how to do it, what would be better, and why it works.Thanks in advance.
date_default_timezone_set('America/Los_Angeles');
require_once('mysql_connection.php');
//analyse data by variable time period.
$hour_position = 45;
$htime = -30;
$date = date('Y-m-d H:i:s');
$date = strtotime($date);
$date = strtotime($htime." day", $date);
$date = date('Y-m-d H:i:s',$date);
$qry = "SELECT `last`,`server_time`,`vol` FROM `btce_btc_ticker` WHERE `server_time` > '$date' AND EXTRACT(MINUTE FROM `server_time`) = $hour_position ORDER BY `server_time` ASC";
$price_history_qry = mysqli_query($con,$qry);
while($result = mysqli_fetch_array($price_history_qry)){
$server_time = $result['server_time'];
$server_time = date("m-d-Y (H:i)",strtotime($server_time));
echo 'Server Hour ='.$server_time.'<br>';
echo 'Two Hour ='.$hourPlusTwo.'<br>';
}
You have your time format like this date('Y-m-d (H:i)
Whatever the time you got from the server you just need to speccify your format and add +2 hour from it.
The Change would be
$YourNewDate = date('Y-m-d (H:i)', strtotime('+2 hour'));
And the Result would be 2015-04-05 (23:05) some value like this format.
Update :
As you want to do the increment from the time you have from db
<?php
$result['server_time'] = '2014-04-18 19:56:00';
$server_time = $result['server_time'];
$hourPlusTwo = DateTime::createFromFormat("Y-m-d H:i:s", $server_time);
echo date('Y-m-d (H:i)', strtotime('+2 hours', $hourPlusTwo->getTimestamp()));
?>
I have date value like so 1369195200000 and I tried to convert it to date like so
$date = 1369195200000;
$result = date("Y-m-d", $date);
but it returns like so 45358-01-25 obviously the year is wrong....is there away to get the correct year? The $date is coming from a database.
The time is in milliseconds, it needs to be divided by 1000.
$date = 1369195200000;
$result = date("Y-m-d", $date / 1000);
I'm trying to subtract the time difference between the current datetime, and a time stated in my database. The datetimes current format is yyyy:mm:dd hh:mm:ss, however for this specific case i just want to subtract the time and not the date so i only want hh:mm:ss to be calculated and then stored into a different variable i can use and format how i want. Is it possible to take a full datetime, break it apart and do a diff on it? I think this is kind of confusing so ask if you need clarification. Here's what i've tried thus far:
<?php
//The time in the database
$classTime = DateTime::createFromFormat('Y-m-d H:i:s', '0000-00-00 18:30:00');
$timein = new DateTime("now", new DateTimeZone('America/Detroit'));
$timein->getTimestamp();
$timeout = $classTime;
$totaltime = $timeout->diff($timein);
$totaltime = $totaltime->format('Y-m-d H:i:s');
$totaltime = date('0000:00:00 H:i:s', strtotime($totaltime));
//I create a new date because i'm storing this time into the database, which can't be done with a datetime.
//FORMAT TIMES
$timein = $timein->format('Y-m-d H:i:s');
$timeout = $timeout->format('Y-m-d H:i:s');
echo "Time in " . $timein . " Time Out " . $timeout . " Total Time " . $totaltime;
?>
This current output returns:
Time in 2013-04-02 14:05:32 Time Out -0001-11-30 18:30:00 Total Time 0000:00:00 00:00:00
But i want it to return something like:
Time in 2013-04-02 14:05:32 Time Out -0000-00-00 18:30:00 Total Time 0000:00:00 03:30:00
Your question isn't very clear and I spent quite a bit of time answering the wrong question until I took a careful look at your code to see what you actually wanted.
As far as I understand it you store the finishing time of a lesson in your database as a MySql Datetime type and you want to find the time remaining between now and the time the lesson ends.
I'll ignore timezones for the purpose of this answer.
You start with
$classTime = DateTime::createFromFormat('Y-m-d H:i:s', '0000-00-00 18:30:00');
To do a meaningful time comparison, the date portion of $classTime needs to be set to today:-
$timeIn = new DateTime();
$year = (int)$timeIn->format('Y');
$month = (int)$timeIn->format('m');
$day = (int)$timeIn->format('d');
$classTime->setDate($year, $month, $day);
You can then do the comparison:-
$diff = $timeIn->diff($classTime);
$diff is now an instance of DateInterval.
We can now echo out the information:-
$start = $timeIn->format('Y-m-d H:i:s');
$end = $classTime->format('Y-m-d H:i:s');
$duration = $diff->format("%Hh, %Im, %Ss");
echo "Time in: $start, Time out: $end, Duration: $duration";
Which at the time I ran the code, gave the following output:-
Time in: 2013-04-05 13:22:54, Time out: 2013-04-05 18:30:00, Duration: 05h, 07m, 06s
I'm writing code to subtract two dates. It is for a contract type thingy, where user gets to see the number of days left for his contract to complete. Something like start_date_time="today" and end_date_time=y where the value of y is retrieved from the database (DATETIME type). It is in the mysql datetime format (yyyy-mm-dd HH:mm:ss).
<?php
include_once '../include/connections.php';
$id =$_REQUEST['uid'];
$result= mysql_query("SELECT * FROM data WHERE uid = '$id'");
$test = mysql_fetch_array($result);
echo $test[14];
echo "<br /><br />";
$today=time();
$enddate=strtotime('$test[14]');
$timediff = $enddate - $today;
$days=intval($timediff/86400);
$remaining=$timediff%86400;
$hours=intval($remaining/3600);
$remaining=$remaining%3600;
$mins=intval($remaining/60);
$secs=$remaining%60;
echo "<br>".$days.' days '.$hours.' hours '.$mins.' minutes and '.$secs.' seconds.';
?>
When I echo $test[14];, I get the date and time as stored in the database which is
(2012-09-26 00:00:00)
When i echo $today; then i get it in this format 1348381896. Now, how do i convert this format to the one retrieved from the db so that i can subtract the 2 dates and get the number of days and time left?
You can use these 2 functions to convert dates to each other,
Use this for timestamp to MySQL datetime:
$timestamp = '1348381896';
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;
Use this one for MySQL datetime to timestamp:
$date = '2012-09-26 00:00:00';
$timestamp = strtotime($date);
echo $timestamp;
Also if you are willing to Subtract your dates in MySQL side, you can use the DATEDIFF and or TIMEDIFF functions:
Also you can work with timestamps in MySQL too, using TIMESTAMPDIFF and UNIX_TIMESTAMP functions ...
You could use PHP's DateTime classes for this:-
$today = new DateTime();
$mysqlDate = "2012-12-15 13:40:20"; //for example
// To match your code this would be $mysqlDate = $test[14];
$mysqlFormat = 'Y-m-d H:i:s';
$endDate = DateTime::createFromFormat($mysqlFormat, $mysqlDate);
$diff = $today->diff($endDate);
echo "You have {$diff->d} days, {$diff->h} hours and {$diff->m} minutes left";
This will give the following output:-
You have 22 days, 6 hours and 5 minutes left
(This will change depending on when you run the code).
$diff is an instance of DateInterval.
date formats acceptable to DateTime::createFromFormat() can be found here http://www.php.net/manual/en/function.date.php