I use a PHP script for a banlist that fetches the date and time, but I have an error and don't know how to convert it to "normal" time.
It lists me only the date : 01.01.1970 um 00:00 Uhr
The script part from this:
//Convert Epoch Time to Standard format
$datetime = date("d.m.Y \\u\\m H:i \\U\\h\\r", $row['expires']);
echo "<td>$datetime</td>";
This is the entry from the mysql db: http://fs1.directupload.net/images/141208/9ugjslm8.jpg
Dont know if it helps?
Have anyone an idea to solve this?
LINK: http://pastebin.com/r0dXg8FX
The row comes from an plugin. for example: $row['name'] , $row ['banner'], $row['reason'] this comes all from the plugin
It lists me only the date : 01.01.1970 um 00:00 Uhr
That simply means you are thinking of $row['expires'] incorrectly. That is not a UNIX Timestamp value and is producing an invalid date. It means the value essentially evaluates to 0, which is Jan 1st 1970 in UNIX time
date() requires you to send a valid Unix timestamp to it (INT 11), is that what you have in database for that field? or it is a date time field?
Try this
echo date("d.m.Y \\u\\m H:i \\U\\h\\r", "2014-10-12"); //invalid
echo date("d.m.Y \\u\\m H:i \\U\\h\\r", time()); //valid: current unix timestamp
Based on this image (that you have given in deleted answer) you have milliseconds. Divide milliseconds with 1000 to get seconds.
Probably something like this:
$datetime = date('d.m.Y \u\m H:i \U\h\r', $row['expires'] / 1000);
But, if you get year 1970, that means that you are on 32bit machine, and your fetched INT is out of range. In this case you could just cut last 3 numbers with string function substr():
$datetime = date('d.m.Y \u\m H:i \U\h\r', substr($row['expires'], 0, -3));
Convert the date to UNIX timestamp first. Try with -
$datetime = date("d.m.Y \\u\\m H:i \\U\\h\\r", strtotime($row['expires']));
echo "<td>$datetime</td>";
Related
I have tried to solve it by extracting the numeric part and then parsed it using date function. But it shows me some old date which I guess is not correct.
$datef = "1490914800000+0100";
$adada = date('Y-m-d H:i:s', $datef);
// Gives date 1987-10-13 18:31:28 which is an old date. Please suggest.
One approach, well-covered by this SO question, is to use the DateTime() function to convert time in seconds since epoch to a date, and then display this date using format(). But there are two caveats with your data. First, you appear to have milliseconds since the epoch, which needs to be converted to seconds. Second, you also have a timezone shift, in hours, tagged to the end. I split your $datef string into two parts, epoch and timezone, then arrive at the number of seconds since epoch.
list($epoch, $timezone) = explode('+', $datef);
$epoch = ($epoch / 1000) + (substr($timezone, 0, 2)*60*60) +
(substr($timezone, 2, 2)*60);
$dt = new DateTime("#$epoch");
echo $dt->format('Y-m-d H:i:s');
Output:
2017-03-31 00:00:00
Demo here:
PHP Sandbox
The time seems to be in milliseconds.
You can add the timezone shift to the seconds. 1 hour = 3600 seconds.
$milliSeconds = intval("1490914800000");
$seconds = $milliSeconds/1000;
$date = date("Y-m-d H:i:s", $seconds);
Im working with an API that is returning times as H:i (e.g. 19:33) but what I need to know is how to get the unix timestamp for that value and also to find out if the time goes into tomorrow i.e 00:00 would be the next day from 19:33
Anyone got a solution?
strtotime() can probably do this:
$api_time = some_api_call();
$timestamp = strtotime("today $api_time");
if ($timestamp < (time()-60)) {
$timestamp = strtotime("tomorrow $api_time");
}
I've following variable containing date in MM-DD-YYYY format. I want to compare this date with today's date. If the date containing in a variable is greater than the today's date I want to echo "Error" and if it is less than or equal to today's date I want to echo "Success".
For this thing I don't want to use DateTime class.
I think using UNIX Timestamp values could be a better option. If yo have any other better and efficient option you are welcome.
Following is the variable containing date in MM-D-YYYY format
$form_data['reg_date'] = '12-11-2014'; //This is today's date i.e. 11th December 2014
If the variable $form_data['reg_date'] contains date greater than today's date(i.e. 11th December 2014) it should give error message otherwise should echo success message.
Thanks.
I've following variable containing date in MM-DD-YYYY format. I want to compare this date with today's date.
You cannot compare string representation of date in format m-d-Y. This format is invalid format, and php will not understand it. Read the manual what date formats are valid.
Best way to compare string dates is to have it in format Y-m-d or convert your string date to unix timestamp integer. But once you have date in Y-m-d format, it is trivial to convert it to unix timestamp, so converting it to timestamp just for comparing is an unnecessary step.
Convert m-d-Y to m/d/Y format, and then to unix timestamp:
$date = '12-25-2014';
$date = str_replace('-', '/', $date);
var_dump($date);
var_dump(strtotime($date));
if (strtotime($date) > strtotime('today')) echo "ERROR";
else echo "SUCCESS";
demo
But this I already explained in answer of your previous/same question (see 2nd link).
I think using UNIX Timestamp values could be a better option. If yo have any other better and efficient option you are welcome.
Other method could be converting format with sscanf() function:
$date = '12-25-2014';
sscanf($date, "%d-%d-%d", $m, $d, $Y);
$date = "$Y-$m-$d";
var_dump($date);
if ($date > date('Y-m-d')) echo "ERROR";
else echo "SUCCESS";
demo
But I would still recommend you to use DateTime class, like I already explained if my previous answer.
Please try below code
if(strtotime(date('d-m-Y')) == strtotime($form_data['reg_date'])){
echo 'Today\'s Date';
}
First you have to convert your date in timestamp value like
$date_time = strtotime("11-12-2014");
and then you can do this to compare today date with the date you have.
$diff = $date_time - time();
This will gives you the date difference in seconds.
Ok so im trying to display a date that i got from a database
Here's what i can display without any formatting: 2012-11-21 19:00:00
what im trying to display is: 2012-11-21 at 19:00:00
below is the code
$expiryDate = date('Y/m/d', $data[0]->dinExpiry);
$expiryTime = date('H:i', $data[0]->dinExpiry);
And this is the error im getting;
Also the date that it displays is wrong
Thanks in advance for the help
Easy solution
You say that you can display 2012-11-21 19:00:00 without any formatting.
So if you can display an arbitrary $date as 2012-11-21 19:00:00, why not just substring it:
$dateToDisplay = substr($date, 0, 11).'at '.substr($date, 11, 8);
and show $dateToDisplay as 2012-11-21 at 19:00:00.
A little more...
It's tricky to answer this without knowing the output of $data[0]->dinExpiry. (I'll assume it gives you a variable called $date).
As #Marc B says, the fact that you're getting 1970 out of it shows that it's not a valid timestamp (hint: it's a string), so it's evaluated to zero seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
You can convert it to a valid timestamp like so:
$expiryDate = date('Y-m-d', strtotime($date));
Or to match your example:
$expiryDate = date('Y-m-d', strtotime($data[0]->dinExpiry));
$expiryTime = date('H:i', strtotime($data[0]->dinExpiry));
I'm trying to do some very basic time math - basically, given inputs of time and distance, calculate the speed. I chose to use strtotime() to convert the time inputs into seconds - but I'm getting some bizarre results.
For example, given this sample program:
<?php
$t1 = strtotime("3:15:00",0);
$t2 = strtotime("1:00:00",0);
$t3 = strtotime("2:00:00",0);
$t4 = strtotime("9:00:00",0);
echo $t1 . "\n";
echo $t2 . "\n";
echo $t3 . "\n";
echo $t4 . "\n";
?>
Why do I get these results?
$ php test.php
-56700
-64800
-61200
-36000
Update:
Since no one said it explicitly, let me explain the bug in the above function. I had assumed that passing a time of zero to strtotime() would cause it to generate time stamps derived from midnight, 12/31/1969, UTC - which sounds odd, but would work for my purposes.
What I hadn't counted on was that strtotime() takes time zones into account when converting strings, and my server is apparently 5 hours behind UTC. On top of that, because of the time zone shift, PHP then interprets the times as relative to the day before the epoch which means it is interpreting my times as occurring relative to December 30th, 1969 instead of the 31st, resulting in negative numbers...
It appears that Eugene is correct - if I want to calculate just the elapsed time, I can't use the built in time functions.
If you want to do something like that, I think you want to just do some math on the time strings themselves and convert them to a number of seconds, like this:
<?php
function hmstotime($hms)
{
list($hours, $minutes, $seconds) = explode(":",$hms);
return $hours * 60 * 60 + $minutes * 60 + $seconds;
}
?>
Apparently with just bare times PHP is assigning the date December 31, 1969. When I ran this:
echo date('F j, Y H:i:s', $t1) . "\n";
echo date('F j, Y H:i:s', $t2) . "\n";
echo date('F j, Y H:i:s', $t3) . "\n";
echo date('F j, Y H:i:s', $t4) . "\n";
I got this:
December 31, 1969 03:15:00
December 31, 1969 01:00:00
December 31, 1969 02:00:00
December 31, 1969 09:00:00
Remember that strtotime returns a UNIX timestamp, which is defined as the number of seconds since January 1, 1970. By definition a UNIX timestamp refers to a specific month/day/year, so despite the name strtotime is not really intended for bare times without dates.
Because strtotime() outputs the number of seconds relative to the second argument (in your case, the Unix epoch (December 31, 1969 19:00:00)).
The negative numbers is expected because "3:15:00" is 56700 seconds before the Unix epoch.
Try it without the second parameter. That's supposed to be a timestamp for the returned time to be relative to. Giving it 0 means you're asking for a timestamp relative to the Unix epoch.
In response to your comment:
It's not documented functionality, but I use strtotime("HH:MM") all the time, and it returns a timestamp relative to the current time. I guess if you want to be sure though, you could do this:
strtotime("3:15:00",time());
strtotime() without a second argument gets the time from the supplied string and fills in the blanks from the current date:
echo date('Y-m-d H:i:s', strtotime("3:15:00"));
-> 2009-06-30 03:15:00
With a second argument it calculates the date relative to the second argument:
echo date('Y-m-d H:i:s', strtotime("3:15:00", 0));
-> 1970-01-01 03:15:00
To calculate the difference between two timestamps in seconds, you can just do this:
echo strtotime("3:15:00") - strtotime("3:00:00");
-> 900
Edit: Of course taking into account which is the bigger number:
$t1 = strtotime("3:15:00");
$t2 = strtotime("3:30:00");
$diff = max($t1, $t2) - min($t1, $t2);
$diff = abs($t1 - $t2);
Or something of that nature...