Based on the definition given on php.net about function date(), I tried to use it in the code like this:
echo"The date is :".date("l, d/m/Y, h:i:s a", time());
But the timestamp doesn't get printed. Why is this? I am unable to understand this part of the function.
To print timestamp just use time()
echo time();
I understood the problem:
I had to written the code properly but since It was not displaying current date and time, I was confused. I set the time zone to Asia/Kolkata. Now its working fine.
Thank you everyone for help.
Related
I am quite sure this has been discussed before but for some reason mine is not working.
I am trying to convert date time to a timestamp.
echo strtotime("18 May 2.50pm");
The above code returns blank.
Any help is highly appreciated. Thanks in advance.
Use php DateTime::createFromFormat to make DateTime
<?php
$date = DateTime::createFromFormat('j M h.ia', '18 May 2.50pm');
echo $date->getTimestamp();
//print_r($date);
?>
Live Demo
Now you can use method available for DateTime .
Note : As you have not given year it will give you result for current year
In Javascript to get the current time for the time stamp I'm using
JSON.stringify(new Date());
Which is giving me following output.
"2016-06-07T10:38:54.767Z"
In PHP how can I get this kind of output in datetime function.
I know this problem can be handled by date function of php. But what I'm looking for is exact argument to that date function so that I can get the output same as given in the question. Which is in UTC timezone. There are similar questions explaining date function but I'm not able to figure out exact arguments for my desired output.
In PHP I tried this
date_default_timezone_set('UTC');
echo date('c');
Which is giving me
2016-06-07T11:56:54+00:00
Thanks in advance.
Reading from the DateTime Class php.net documentation,
You can use $yourdate->format('c')
http://php.net/manual/en/datetime.format.php
See here for date format details :
http://php.net/manual/en/function.date.php
I'm having a problem with the PHP date function which I've never had a problem with before.
The timestamp is entirely correct, however for some bizarre reason date() is outputting a time which does not correspond.
I have the following timestamp (and this is definitely the correct one - when I echo it out onto the page, as well as in the database, it shows as being correct):
464400
Yet when I use the following line of code:
<?php echo date("H:i",$timestamp); ?>
I'm getting a time of 4 am? If I paste the timestamp into a timestamp converter website, then it shows the time should in fact be 9am.
I'm completely stuck, this has never happened to me before & this problem has only just come up recently - the code hasn't been changed and everything seemed to be working correctly before.
Does anyone have any experience with this issue? Any help would be appreciated.
That time stamp is is 9am GMT timezone, if you are in another timezone then you will need to adjust it accordingly.
http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('Europe/London');
or even better in your php.ini
http://php.net/manual/en/datetime.configuration.php
date.timezone="Europe/London"
Or you can use more specifically GMT instead of Europe/London (which has DST)
try this method will work
for time zone
http://php.net/manual/en/timezones.php
code
<?php
date_default_timezone_set('Asia/Kolkata');
$dt2=date("Y-m-d H:i:s");
echo $dt2;
?>
try this
// set default timezone
date_default_timezone_set('UTC');
//define unix timestamp
$timestamp = 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);
Try this converter too http://freeonlinetools24.com/
For time zone: https://www.php.net/manual/en/timezones.php
date_default_timezone_set('America/Chicago');
echo date("m/d/Y h:i:s A");
I have tried using php date function() like as follows
$date=date('Y-m-d').' '.date('H:i:s');
echo $date;
the output displayed is 2013-04-03 09:04:02.. but my system is 02:49 pm...
What time is being displayed for me? I tried changing the internet timing even then I am getting the same answer ?
First off, it is not necessary to use the date function twice. This will do the same thing:
echo date('Y-m-d H:i:s');
Second, you need to set PHP's date.timezone. This can be done in the php.ini file, but it can also be done using the date_default_timezone_set function, like this:
date_default_timezone_set('Europe/Amsterdam');
The string that you have to put in can be found in the documentation.
It may also be worth noting that you can tell the date function to use any time. This is done by passing in a *nix timestamp as the second argument. For example:
// One week ago from now
echo date('Y-m-d H:i:s', time()-604800);
It will show server's time only. If possible compare with your server time. If you want to use local machine's time you need to go with JAVASCRIPT.
And another suggestion,
You don't have to use individually to display date & time. You can achieve this in a single statement like this.
$date=date('Y-m-d H:i:s');
You will get the same format 2013-04-03 09:04:02
check for your system timezone and your default timezone in php by opening phpinfo()
I have a simple date time in the format 10/20 4:30PM. I want it to display in the format
10/24 1:30PM -007. I am saying date('Y-m-d H:i:sT', $time_recieved) assuming T is for timezone. But It is still not giving the time zone. What might be the reason? Is there any other format I am missing?
What you want is O not T I believe.
Check the PHP date page for all the format listings possible.
http://php.net/manual/en/function.date.php
You can have a look at the official manual of PHP : date ();
http://www.php.net/manual/en/function.date.php
I tested that and I used on my server with :
$time = time();
echo date('Y-m-d H:i:s T', $time);
it shows the timezone perfectly. Check $time_received, it might be an erroneous number, try with local time with timeĀ“() function to ensure that it's OK.
Good luck