i am trying to make php timestamp function i make it like this
My code example
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
but i want php timestamp in this format
Saturday, August 18, 2018 10:55:34 AM GMT+05:30
output:-1534569934
what i need to changes in my code
Here is what i tried,
<?php
$t = time();
echo(date("l, M d, Y h:i:s A",$t).' GMT '.date("O",$t));
I hope this solves your problem :-)
According to :
i want php timestamp in this format
Saturday, August 18, 2018 10:55:34 AM GMT+05:30
you can format your timestamp as this:
date('l ,F d, Y h:i:s A \G\M\T P');
you also said :
i am trying to make php timestamp function
First ,i don't see any function declaration nor return statement so you didn't try to make a function.However according to the format above and the fact that you want a custom timestamp function, you can build it this way:
function custom_timestamp(){
return date('l,F d,Y h:i:s A \G\M\T P');
}
Then you can use it anywhere as timestamp and based on your own logic.
You can try this:
<?php
$timestamp=time();
echo(date("F d, Y h:i:s A", $timestamp));
?>
Related
Can anybody tell me why strtotime() seems to be adding 1 day? This seems to only happen in the late afternoon (something like 7 or 8 PM), otherwise it says the correct day.
echo date('m/d/Y h:i:s a', time());
Output:
12/21/2015 08:34:43 pm
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
Output:
Tuesday, December 22nd, 2015
I would like the above output, however, I want today's date (the 21st not the 22nd).
Use date instead of gmdate.
You are using gmdate() which gets the date in UTC. The problem only happens late in the afternoon/evening because at those times it really is the next day in UTC time.
You're also doing too much work - you can simplify that line of code to this:
// echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
echo date('l, F jS, Y');
Otherwise you've created a timestamp from a time string based on the current time stamp. You could just leave the second parameter to date empty and the current time "now" is assumed.
It is also very important to make sure you are calling date_default_timezone_set somewhere or that you have it configured in your php.ini.
This detail in your code...
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
(= the "gmdate") will always return Greenwich Mean Time (GMT), which is London/UK.
So change that to date(....
And add date_default_timezone_set('America/New_York'); anyway...
Decided to ultimately use:
$date = new DateTime(date('Y-m-d'), new DateTimeZone('America/New_York'));
$timestamp = $date->format('U');
$date = gmdate('l, F jS, Y', $timestamp);
based on Alexander's comment.
Trying to convert a date with the format Jul 27, 2015 5:42:05 PM This is the current way that I'm trying to create the date from the format that I've been provided.
$newDate = new DateTime::createFromFormat('m d, y H:i:s', $game->createDate);
It doesn't like the way that I'm currently doing it. Do I need to try and rework the way that the date comes to me?
This is how I am printing it currently echo date_format($newDate, 'Y-m-d');
createFromFormat is a static method. You don't new it:
$new = DateTime::createFromFormat(...);
it'll do the new business for you internally.
You need to remove the new keyword. In addition, your format string doesn't match the date string you gave. This should work:
$newDate = DateTime::createFromFormat('M d, Y h:i:s a', $game->createDate);
I'm using a database where in the table I have the time after it was saved using time()
Is there any way formatting it to human readable way (date and time)?
Thanks
yes, you can use date function for that.
echo date("F j, Y, g:i a", $timestamp);
Output will be in following format:
// March 10, 2001, 5:16 pm
you use this query:
SELECT DATE_FORMAT(timestamp, '%M %d, %Y %h:%i:%s %p') as mydate
see DATE_FORMAT for more info
Check here
PHP Date() Documentation
There's a table with every option
An example:
date('Y m d')
prints
2013 07 19
If you have trouble with the data displaying from some of the other answers there is also this function strtotime which may help parse it.
echo date("F j, Y, g:i a", strtotime($timestamp));
After querying the database for the timestamp, pass it through the function formatTime. This creates a new DateTime object in php called $data that can also easily be manipulated if you need. The examples below formats the date in two different ways, both work.
Procedural Style:
function formatTime($millis) {
$date = new DateTime($millis);
return date_format($date, 'l, jS F Y \a\t g:ia');
}
Object Oriented Style:
function formatTime($millis) {
$date = new DateTime($millis);
return $date->format('l, jS F Y \a\t g:ia');
}
The format for date can be found at this link:
http://php.net/manual/en/function.date.php
Cheers!
All,
I have a date format in the following output that I get from Tumblr:
2013-02-25 18:00:25 GMT
I'd like to convert this to a date format I want using the date function. I tried the following:
$date = date("F d, Y",$tumblr_posts->date);
echo $date;
When I did this, the output was "January 01, 1970" which is obviously not right. Any ideas on how to fix this?
Thanks!
You need to use the strtotime function like this.
$date = date("F d, Y", strtotime($tumblr_posts->date));
To find how to parse your date use the PHP date function reference.
I have dates currently in this format: yy-mm-dd (e.g. 2011-11-18)
I want them in this format: Friday 18 November 2011
I've tried reading through the PHP documentation manual, but I can't see how to manage dates in the format that I have. If the date needs to be in a different order I can arrange that, but I'm a bit stuck at the meoment.
Any help would be much appreciated.
Use PHP5s new date classes. Much cleaner:
$date = DateTime::createFromFormat('Y-m-d', '2011-11-18');
echo $date->format('l d F Y');
date('l j F Y', strtotime($date));
Just use starttime to change the the dates in many formats using this link.
echo date('l d F Y');
gives you the date format you want.
This was all in the manual you yourself linked.
just use strtotime to get back a timestamp and then use date() to format that:
$date = '2011-11-18'; // your date
$timestamp = strtotime($date); // convert to a timestamp
$new_date = date('l j F Y',$timestamp) // format timestamp
echo $new_date;