I am having a strange problem, maybe you can help:
I'm trying to convert a date to GMT time, and this is what I'm doing:
$date = '2010-05-27 23:02:01';
$gmt_date = gmdate('Y-m-d H:i:s', $date );
but the yield of $gmt_date is this:
1970-01-01 00:33:31
What am I doing wrong?
gmdate expects the second parameter to be an integer (the number of seconds from the unix epoch)
Try this:
$date = '2010-05-27 23:02:01';
$gmt_date = gmdate('Y-m-d H:i:s', strtotime($date) );
You need to convert your $date into a timestamp. You can do this using the strtotime() function. Depending on timezones, you may want to set the php timezone or append a timezone to the $date string before calling the strtotime function.
$gmdate_str = gmdate('Y-m-d H:i:s', strtotime($date));
Related
I am trying to convert this time format - 1480550400000+0000 in Y/m/d date format using php date('Y/m/d',1480550400000+0000); but its not working. How can I make it work?
You timestamp has microseconds, so first remove it.
<?php
$timestamp = 1480550400000+0000;
$timestamp = intval($timestamp/1000);
$date = date("Y/m/d", $timestamp);
echo $date;
output: Check the live demo.
ei#localhost:~$ php test.php
2016/12/01
$dig_date= 1480550400000+0000;
$date = DateTime::createFromFormat('YmdGis', $dig_date);
echo $date->format('Y-m-d G:i:s');
Please note that the second parameter should be time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Looks like you have entered milliseconds instead.
PHP date()
string date ( string $format [, int $timestamp = time() ] )
timestamp
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
Try this:
echo date('Y/m/d',1480550400+0000); // 2016/11/30
#Symplifys try this:
<?php
$date = date("Y-m-d", "1480550400000+0000");``
echo $date;
?>
Remember to put timestamp in double quotes.
Try this code at http://phpfiddle.org/
Solved I just divided this with 1000. $date = date("Y-m-d", $timestamp/1000); and it worked
Thanks
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
}
I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables
// unix
$date = 1436713200
// string
$time = '15:00';
to get a date format 2015-07-12 15:00 but failing, using this
$newdate = date('Y-m-d H:i:s', $date.' '.$time);
I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.
I would suggest you to use DateTime instance to avoid timezone issues:
$d = date_create('#1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00
You do not have to provie the $time variable. Unix time is a full date with time.
Use:
$newdate = date('Y-m-d H:i:s', $date);
Use this
$date = date('Y-m-d','1436713200');
// string
$time = '15:00';
echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));
Given a date string formatted Y-m-d (2014-4-11 for example), how can I get a UNIX timestamp of 12am the beginning of that day? Would it involve date_parse_from_format()?
Thanks
You can simply use strtotime()
$date = "2014-04-11";
$timestamp = strtotime($date);
which inturm gives you -
$d = date('Y-m-d H:i:s', $timestamp); // 2014-04-11 00:00:00
Try online conversion to test - http://www.onlineconversion.com/unix_time.htm
Maybe is a dumb question, but I can't find the proper function:
I have a string date like 04/05/2012
How I can pass to this format: 2012-04-05 20:18:11 to insert in mySQL?
Combination of date and strtotime ...
$my_date = date('Y-m-d H:i:s',strtotime('04/05/2012'));
$dateTime = new DateTime($your_time_string);
echo $dateTime->format("Y-m-d H:i:s");
Time can not be 'made' on its own. If you're however inserting the current date-time setting, then the following code will be used:
$dt = date( "Y-m-d H:i:s", time() );
You may use dleiftah's statement, but the time in that case will always be 00:00:00