How can I covert milliseconds (unix timestmap) to utc php - php

I am creating web services for android in php, they are sending time in unix timestamp (milliseconds).
Now I need to convert this in utc timestamp and compare with mysql created_at.
I have tried that:
$time = 1443001794;
$seconds = $time / 1000;
echo date("d-m-Y", $seconds);
But it always returns '17-01-1970'.

Well if you only want to compare with mysql timestamp then you can do like this:
$result = strtotime($mysqlCreateAt);
strtotime will convert your timestamp in unix timestamp, and then you can compare with any time you want.
If you want to convert this to utc, try this:
$date = new DateTime();
$date->setTimestamp(1443001794);
var_dump(gmdate('Y-m-d H:i:s', strtotime($date->format('Y-m-d H:i:s'))));
It will give to UTC timestamp

MYSQL date format is "yyyy-mm-dd":
So simply use this:
$time = 1443001794;
echo date("Y-m-d", $time);
If You want date with time . Then Try this
$time = 1443001794;
echo date("Y-m-d H:i:s", $time);

If you want to compare then you can just use strtotime for that.
$db_time = strtotime($created_at); // it will convert your db created at in unix
if($unixtime > $db_time){
// your code here
}

Related

Difference between timestamps in minutes in PHP

I want to get difference between 2 timestamps of format Y-m-d H:i:s in minutes in PHP.
The code used is,
$currentDate = date('Y-m-d H:i:s');
$userLastActivity = date($date);
$timeLapse = (($currentDate - $userLastActivity)/60);
Here, $date is obtained from database.
The value of $timeLapse is 0 in output. Please help.
Try using unix timestamp. Practically it measures the time in seconds from 1/1/1970 and it's a lot easier to use and understand than a php object.
$currentTimestamp = new DateTime()->getTimestamp();
$userLastActivity = date($date)->getTimestamp();
$timeLapse = (($currentDate - $userLastActivity)/60);
You should have the time saved as timestamp on the server too, in that case you could use the $date directly as a number, with no need for a conversion. And also, because it's universal, you can pass it around to javascript or any other language without any worries for conversion
Use strtotime to parse textual datetime into a Unix timestamp and substract $userLastActivity from $currentDate and divide by 60.
See if this helps -
<?php
$currentDate = strtotime(date('Y-m-d H:i:s'));
$date = "2016-10-11 02:40:50";
$userLastActivity = strtotime($date);
echo round(abs($currentDate - $userLastActivity) / 60). " minutes";
?>
For more details :strtotime
Change these 2 lines for a start, as I don't think dividing dates by 60 is gonna work to well.
$currentDate = time();
$userLastActivity = strtotime($date);
That way you have time stamps and not dates (string)

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

Add minutes to current time

I am trying to add minutes to current date but it returns strange results
date_default_timezone_set('Asia/Karachi');
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime("+10 minutes", $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
echo $currentDate;
I am getting Output
01-01-1970 05:50:00
07-19-2013 20:25:23
It should return
07-19-2013 20:35:23
07-19-2013 20:25:23
After this I need to query to database so date format should be same. Database column is of string type.
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
Probably the minimalist way would be:
date_default_timezone_set('Asia/Baku');
$packageEndDate = date('Y-m-d H:i:s', strtotime('+10 minute'));
echo $packageEndDate;
Output (Current time in my city at the time of writing):
2017-07-20 12:45:17
Try this:
$now = time();
$tenMinFromNow = date("m-d-Y H:i:s", strtotime('+10 minutes', $time));
$tenMinsFromNow = (new \DateTime())->add(new \DateInterval('PT10M'));
Will leave you with a DateTime object representing a time 10 minutes in the future. Which will allow you to do something like:-
echo $tenMinsFromNow->format('d/m/Y H:i:s');
See it working
PHP version >= 5.4 I'm afraid, but you should be using at least that version by now anyway.
Pakistan, which is the localisation explicitly set, uses "DD-MM-YYYY" format dates so the problem occurs when you cast the date into a string of "MM-DD-YYYY". This American format of date is not parseable by the Pakistan localisation.
If you still want to keep the round-trip to a string and back, use DD-MM-YYYY or the ISO datetime format.
While this is the only (current) answer which actually explains your original issue, I recommend the code be refactored as others have demonstrated.

Timestamp to yyyymmdd hhmm but only keep hhmm

I have a timestamp in the database. With the following code I can format it to the right date:
$datefrom=mysql_real_escape_string($record['projectdatefrom']);
$date1 = date("Y/m/d", $datefrom);
Then I give the input vield the value="$date1.
Now I have another field for the H:i, so I'd like to have them seperate from each other.
Can can I cut the Y/m/d of the $date1 and only return the H:i?
Was trying doing things like this: $datetest = date("H:i", $datefrom); but no success.
$datefrom will need to be a UNIX timestamp. strtotime() can be useful for generating one off a plain-text or MySQL-style date string.
There is no need to escape the string after it was returned from the DB.
Therefore:
$date = date("Y/m/d", strtotime($record['projectdatefrom']));
$time = date("H:i", strtotime($record['projectdatefrom']));
Or using DateTime:
$dt = new DateTime($record['projectdatefrom']);
$date = $dt->format('Y/m/d');
$time = $dt->format('H:i');

How to convert MySQL time to UNIX timestamp using PHP?

There are a lot of questions that ask about 'UNIX timestamp to MySQL time'. I needed the reversed way, yea... Any idea?
Use strtotime(..):
$timestamp = strtotime($mysqltime);
echo date("Y-m-d H:i:s", $timestamp);
Also check this out (to do it in MySQL way.)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
You can mysql's UNIX_TIMESTAMP function directly from your query, here is an example:
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Similarly, you can pass in the date/datetime field:
SELECT UNIX_TIMESTAMP(yourField);
From one of my other posts, getting a unixtimestamp:
$unixTimestamp = time();
Converting to mysql datetime format:
$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);
Getting some mysql timestamp:
$mysqlTimestamp = '2013-01-10 12:13:37';
Converting it to a unixtimestamp:
$unixTimestamp = strtotime('2010-05-17 19:13:37');
...comparing it with one or a range of times, to see if the user entered a realistic time:
if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}
Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:
if(ctype_digit($_GET["UpdateTimestamp"]))
{...}
$time_PHP = strtotime( $datetime_SQL );
Instead of strtotime you should use DateTime with PHP. You can also regard the timezone this way:
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $mysqltime, new DateTimeZone('Europe/Berlin'));
$unix_timestamp = $dt->getTimestamp();
$mysqltime is of type MySQL Datetime, e. g. 2018-02-26 07:53:00.
Slightly abbreviated could be...
echo date("Y-m-d H:i:s", strtotime($mysqltime));

Categories