i have a unix time 1410938094654
I want to convert this to date time format
$dt = new DateTime('#1410938094654');
$dt->setTimeZone(new DateTimeZone('Asia/Tehran'));
echo $dt->format('F j, Y, g:i a');
But the wrong time will returned
06-11-2000 00:30:54
correct time is GMT: Wednesday, September 17, 2014 7:14:54.654 AM
also my php.ini
date.timezone = "Asia/Tehran"
gmdate also returns the same date (wrong date)
DateTime accepts unixtime in seconds, and you have it in milliseconds, so you must divide timestamp to 1000 before creating new DateTime object.
$dt = new DateTime('#1410938094654');
gives: November 6, 46680, 12:30 am
$dt = new DateTime('#1410938094');
gives September 17, 2014, 7:14 am
Convert it to seconds before using.
Related
I have date stored in database in this format frankly i dont know the timezone of this format
2016-05-26T11:35:00.000Z
but i want to display date in this format
May 27, 2016 12:00 am
Here is code
$date = substr($query_result->post_date, 0,10);
$date = date_create($date);
date_format($date,"F j, Y g:i a");
It shows date correctly but it always shows 12:00 am with date.
Like if date is
2016-05-26T11:35:00.000Z
then it should display
May 26, 2016 11:35 am/pm
Thanks in advance
While shrinking your datetime string, you lose time and timezone information. Time 00:00 and system timezone is then applied to your datetime, and you get wrong result because of that.
$date = date_create($query_result->post_date);
date_format($date,"F j, Y g:i a");
I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();
How do I convert "2 October, 2012 12:28" to a comparable timestamp?
Goal is to get a timestamp value on which I can use comparable operators like <, >, = and !=.
Thanks for your help
You can try using DateTime::createFromFormat
$dateTime = DateTime::createFromFormat("d F, Y g:i", "2 October, 2012 12:28");
var_dump($dateTime->getTimestamp());
Output
int 1349173680
You can also add timezone
$dateTime = DateTime::createFromFormat("d F, Y g:i", "2 October, 2012 12:28", new DateTimeZone('UTC'));
var_dump($dateTime->getTimestamp());
Output
int 1349180880
You can get it to work with two calls to strtotime:
$date = strtotime("2 October, 2012");
// Now use the current date to get the time:
$timestamp = strtotime("12:28", $date);
You can separate the date segments with explode.
I have the following timestamp:
1342259667654
which when converted with http://www.epochconverter.com/ gives:
Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2
And that is the correct time, but when using:
echo date("Y-m-d H:i:s", 1342259667654);
I get the following date:
1904-07-24 10:22:47
How can I get with PHP the exact date out of this time stamp?
Your timestamp needs to be divided by 1000:
echo date("Y-m-d H:i:s", 1342259667654/1000);
$timestamp = 1342259667;
$dt = new DateTime("#$timestamp"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
You can also do it this way.
The value 1342259667654 is actually in miliseconds, while PHP's date() function is unable to handle miliseconds value. Hence the weird output.
I need to display the time but it must start from 00:00:00? I've got the following but it uses the current time.
print(date("H:i:s"));
As an alternative to mktime(), try the newer DateTime class, eg
$dt = new DateTime;
$dt->setTime(0, 0);
echo $dt->format('H:i:s');
// Add one hour
$dt->add(new DateInterval('PT1H'));
echo $dt->format('H:i:s');
Update
The flexibility of DateInterval makes this a very good candidate for a timer, eg
// add 2 years, 1 day and 9 seconds
$dt->add(new DateInterval('P2Y1DT9S'));
Use mktime() if you want to start with midnight for the current date:
<?php
echo date('l jS \of F Y h:i:s A',mktime(0,0,0));
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
http://codepad.org/s2NrVfRq
In mktime(), you pass arguments for hours, minutes, seconds, month, day, year, so set hours, minutes, seconds to 0 to get today at midnight. (Note, as Phil points out, mktime()'s arguments are optional and you can leave month, day, year out and it will default to the current date).
The mktime() function returns a unix timestamp representing the number of seconds since the unix epoch (January 1, 1970). You can count up from it in seconds or multiples of seconds.
<?php
// $midnight = mktime(0,0,0,date('m'),date('d'),date('Y'));
// The above is equivalent to below
$midnight = mktime(0,0,0);
echo date('l jS \of F Y h:i:s A',$midnight)."\n";
echo date('l jS \of F Y h:i:s A',$midnight+60)."\n"; // One minute
echo date('l jS \of F Y h:i:s A',$midnight+(60*60))."\n"; // One hour
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
Friday 14th of October 2011 12:01:00 AM
Friday 14th of October 2011 01:00:00 AM
http://codepad.org/FTr98z1n
date() uses the current time when you don't pass in an explicit timestamp. See the optional argument in the date documentation.
If you want to explicitly format midnight, use:
date("H:i:s", mktime(0, 0, 0));
try to use this syntax:
print(date("H:i:s", 0));
or
print(date("H:i:s", 10)); // 10 seconds