MICRODATE in php - 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

Related

Can't store timestamp in milliseconds into an integer

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.

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

Convert date with GMT php

I get date in this format: 2016-09-23T19:15:00+02:00.
How can I with php get format: 2016-09-23 21:15.
I tried:
date('Y-m-d H:i', strtotime('2016-09-23T19:15:00+02:00');
But i get
2016-09-23 19:15.
Give this a try:
function parseDate($date){
$parts = explode("+", $date);
if(isset($parts[1])) {
$ph = (int)explode(":", $parts[1])[0];
// take "02:00", extract "02" and convert to int. result: 2
return date("Y-m-d H:i:s", strtotime($parts[0]." + $ph hour"));
}
return date("Y-m-d H:i:s", strtotime($date));
}
echo parseDate("2016-09-23T19:15:00+02:00");
// output 2016-09-23 21:15:00
echo parseDate("2016-09-23T19:15:00");
// output 2016-09-23 19:15:00
You can check it on 3v4l.org
Note: this avoids using date_default_timezone_set() which sets the default timezone used by all date/time functions. If your application is already setting a default timezone, overriding it for a simple parsing is not really recommended.

Avoid displaying 1970-01-01 when strtotime is false

How can I avoid displaying 1970-01-01 when the database field is empty? Since my webpage is in Europe I need to display the date like this dd.mm.YYYY using this code:
<?php echo date("d.m.Y", strtotime($row['date'])); ?>
But where the db fields are empty I get the 1970-01-01. I understand why this is happening, but is there a way to avoid this and display a blank field on the webpage insted?
Original format of the date in the db is YYYY-mm-dd
You could do something like this:
$date = empty($row['date']) ? "" : date("d.m.Y", strtotime($row['date']));
echo $date;
Try this:
<?php
if(isset($row['date']) && $row['date'] !="")
{
$date = date("d.m.Y", strtotime($row['date']));
}
else {
$date = "";
}
echo $date ; ?>

Why isn't this PHP date comparison behaving as I expect?

I have a MySQL database with a list of dates. I want to output all these dates, provided they occur after today, into a page. The dates are stored in the database in DATE format, as Y-m-d.
I've got the following code (excluding the query etc):
$dateToday = date('Y-m-d');
do{
$dateCompare = new DateTime($row['date']);
if ($dateCompare > $dateToday){
echo '<p>'.$dateCompare -> format('Y-m-d').'</p>';
} else {
echo '<p>FALSE</p>';
}
}while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
But this just outputs all the dates, including one I have set in the past for testing purposes. What am I doing wrong?
$dateToday is a string. $dateCompare is a DateTime object.
You should use strtotime() function.
http://www.w3schools.com/php/func_date_strtotime.asp
Try and convert the date from myssql to a datetime object and output.
$changetime = new DateTime($time, new DateTimeZone('UTC'));
ECHO $changetime->format('m/d/y h:i a');
// list of timezones http://us1.php.net/manual/en/timezones.php
I actually use this to output all my MYSQL datetime data - allows me to convert to any timezone. Note, this will assume your datetime is in UTC - you should convert to your timezone.
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt as $row) {
$time = strtotime($row['date']);
if ($_SERVER['REQUEST_TIME'] - $time) {
echo '<p>'. date('Y-m-d', $time) . '</p>';
} else {
echo '<p>FALSE</p>';
}
}

Categories