Can't store timestamp in milliseconds into an integer - php

I have result of CQL query in $result and I want to print date time in string format from timestamp. My code is as follows:
foreach ($result as $row) {
$mil = $row['datetime'];
$seconds = $mil/1000;
echo date("d/m/Y H:i:s", $seconds);
}
Output of :
echo $mil;
is correct millisecond value
of timestamp
And output of :
echo (gettype($mil));
is 'object'.
As I need to divide millisecond value, I need millisecond value to be stored in integer variable. How to do that?

This just works fine
echo date("d/m/Y H:i:s", (1518892182000/1000));
Use var_dump($mil) to check what datatype it is.
Make sure that you passing an integer value to $mil or typecast the value always to an integer.
$mil = (int) $row['datetime'];

$dateTemp = date('Y-m-d H:i:s', $row['datetime']->time());
or
$dateTemp = $row['datetime']->toDateTime()->format('Y-m-d H:i:s');
works.

Related

MICRODATE in php

I have a problem, the date is saved in the form 1556447923594 (microdate). I want to retrieve this date from the database and display it in php.
$datetime = gmdate("Y-M-D G:i:s", $row['time']);
if($row['time'] == "0"){
echo "<td>Permban</td>";
}
else
{
echo "<td>$datetime</td>";
}
And I have = 51290-Aug-Fri 18:38:20 not this 2019-04-28 01:22:12
You just need to divide the value by 1000 to convert it to seconds so it can be used by gmdate. Also, to get the output in the form you want, you should change the format string to Y-m-d H:i:s:
$row['time'] = 1556447923594;
echo $datetime = gmdate("Y-m-d H:i:s", $row['time']/1000);
Output:
2019-04-28 10:38:43
Demo on 3v4l.org

Working with TeeChart DateTime on XAxis

I have an Oracle query which returns date string in the format Y-m-d H:i:s and I need to pass them to the Series::AddXY method. How can I do that?
The "CandleChart.php" example in the Features demo shipped with the product uses DateTimes on the horizontal axis.
Here a variation:
<?php
//Includes
include "../../../../sources/TChart.php";
$chart1 = new TChart(600,450);
$chart1->getChart()->getHeader()->setText("Candle Style");
$chart1->getChart()->getAspect()->setView3D(false);
// Clip Series points
$chart1->getChart()->getAspect()->setClipPoints(true);
$chart1->getChart()->getLegend()->setVisible(false);
// Add Candle data using doubles for date values
$today = time();
$day = 86400;
$hour = 3600;
$chart1->getAxes()->getBottom()->setIncrement(DateTimeStep::$ONEMINUTE);
$chart1->getAxes()->getBottom()->getLabels()->setDateTimeFormat('d/m/Y H:i:s');
$chart1->getAxes()->getBottom()->getLabels()->setAngle(90);
$candle=new Candle($chart1->getChart());
$chart1->setAutoRepaint(false);
for ($i=$today;$i<($today+$hour);$i+=60) {
$candle->addCandle($i,rand(0,100),rand(0,100),rand(0,100),rand(0,100));
}
$chart1->setAutoRepaint(true);
$chart1->doInvalidate();
$chart1->render("chart1.png");
$rand=rand();
print '<font face="Verdana" size="2">Candle Chart Style<p>';
print '<img src="chart1.png?rand='.$rand.'">';
?>
The problem is that I do not have constant time intervals and I can not use a "time machine" as in the Candle example .
The time (X value) I have comes from an Oracle query:
$query = "SELECT ptm.IDENTIFICACAO,
mtr.SERIAL,
TO_CHAR(rtu.DATAHORA, 'yyyy-mm-dd hh24:mi:ss') AS DATAHORA,
So the DateTime value is a string in the PHP date format : Y-m-d H:i:s, which I need to convert to TChart values. I do not know if I am full correct but it
seems that DateTime values should be entered as float values (Unix Timestamp)
So I am converting them as follows:
while( ($row = oci_fetch_array($stmt, OCI_ASSOC)) != false ){
$thetime = DateTime::createFromFormat('Y-m-d H:i:s', $row["DATAHORA"]);
if($thetime)
$tchart->getChart()->getSeries(0)->addXY((float) $thetime->getTimestamp() , $row["ENERTOT"] / 1000);
}
++$rowCount;
}
I hope this can help someone else.
Best regards.

Comparing two dates in PHP and MYSQL

I want to compare two dates and time values in PHP. One date is coming from MySQL, and second one is the current date. I want to run some code when both dates are the same. I tried the code below, but condition satisfies any time which is wrong.
$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
//I want to run some code here
}else{
}
What is wrong with the code? I also tried to covert both dates with strtotime() before comparing, but it gave me the same issue. The above condition satisfies any time even if both dates are different.
Try this :
$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if(strtotime($current_datetime) == strtotime($send_date)){
//I want to run some code here
}else{
}
Hope it helps !!!!
One way is to fetch the Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) from MySQL, then operate on the numbers:
$row = get_db_row("SELECT UNIX_TIMESTAMP(send_date) AS send_date_ts
FROM table WHERE $condition");
$hours = (int) ($row['send_date_ts'] / 3600);
$current_hours = (int) (time() / 3600);
if ($hours == $current_hours) {
// current hour
}
Timestamps are convenient because:
there is no need to take the format into account;
operations on numbers are usually faster;
the code looks cleaner.
Try this. On my server is working just great I've got something else because they aren't equal. Date which I receive from database is type datetime format 2015-04-13 09:03:49
<?php
$current_datetime = strtotime(date('Y-m-d H:i'));
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
//I want to run some code here
echo 'something';
}else{
echo 'something else';
}
Output:
echo $current_datetime . '<br/>';
2016-10-17 09:19
echo $send_date .'<br/>';
2015-04-13 09:03
// result
something else

What is the best way to add days to the date in PHP?

I have a date value with time. (27/4/2014 18:00:00)
In addition to it, I have a string that consists of days/hours/mins. (10/7/0).
In the end, I need to sum the two values making one date.
In this example, the sum of the values is 37/4/2014 28:7:00.
So the desired result would be 8/5/2014 4:7:00. What is the best way to get this result in PHP??
function($date, $addon)
{
$date1 = strtotime($date);
$explode = explode('/', $addon);
$date2 = ($explode[0]*86400) + ($explode[1]*3600) + ($explode[2]*60);
return date('j/n/Y H:i:s', ($date1+$date2));
}
I'd convert them to timestamps then add them and convert them back:
$time = strtotime($time1) + strtotime($time2);
echo date('m/d/Y h:i:s a', $time);

php time and date function

I have a quickie here,
Whenever I try to echo out the time in hours:min:sec using the date() function everything works perfect.
But when I try echoing it out using a variable with a value, it always adds up 2 hours.
Take a look at the code:
$time = time();
$past = 120;
//this works perfectly
echo $time = date("H:i:s",$time);
//but this doesnt. it adds 2 hours.
echo $time = date("H:i:s",$time);
string date ( string $format [, int $timestamp ] )
On second using of "date" function, second param is string. Like that 01:01:01. But it must be integer. So converting 01:01:01 to integer; it will be "0". What's your purpose?
Watch out what You are doing:
//You are assigning a string to $time variable
echo $time = date("H:i:s",$time);
//second call - trying to format date from unix timestamp, which actually is a string with some hours, minutes and seconds
echo $time = date("H:i:s",$time);
EDIT
Maybe You mean this?
$time = time();
$past = 120;
echo date("H:i:s",$time);
echo date("H:i:s",$time - $past);

Categories