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");
Related
I have a query that inserts date in this format
$time = date("m-d-y");
But when I Fetch and output date it shows wrong date. real: October 2020 but outputs: January 1970
$time = $row1['time'];
$newDate = date('F Y', strtotime($time));
echo $newDate;
How do I output date in this format: 20 OCT, 2020
Try not to store date/time using varchar datatype. If you can change it, please do. However, if you can't change database structure, you can use date_create_from_format() to create a DateTime object from a custom format:
echo date_create_from_format('m-d-y', "10-29-20")->format('F Y');
Output:
October 2020
Edit: Change the format part to ->format('d M, Y') to match your desired format
Output:
29 Oct, 2020
Note: Please don't mark this as duplicate This is not related to other questions as they all have incorrect timezone while I don't and the time getting displayed is literally January 01, 1970, 05:00:00 although it is May 19, 2020, 12:19:00 right now.
I am using the PHP date function for this
<?PHP
date_default_timezone_set("Asia/Karachi");
// Converting $timestamp to human readable format
$date = date("F d, Y h:i:s", $timestamp);
?>
If you don't include $timestamp it will give you the current date, which is what you want I assume:
$date = date("F d, Y h:i:s");
See the manual for more details.
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.
I have a feed which gives feed in the following format: "Fri 14 Oct"
I want to see if today's date matches the date from the feed. My problem is the format of today's date/
$today = date("d m");
This outputs 17 10.
What is the best way to format $today so that it outputs Day (shorthand) space date (number) Month (shorthand) ?
how about:
$today = date("D j M");
As explained in date() reference manual.
Anyway you should be aware of timezone issues unless you are 100% sure that your server is in the same timezone of the feed you are comparing.
I would follow a different approach though, you can parse the feed's date using DateTime::createFromFormat() which also understand timezones, and then compare it with today's date.
$today = date("D d M");
PHP Date Documentation
<?php
// Prints the day
echo date("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>
For more details, please visit http://www.w3schools.com/php/func_date_date.asp
I have this line of code I wrote to create a formatted timestamp on emails from my contact us page.
It is working fine, but I'm wondering if it is written poorly and could be reduced into more efficient code? It feels wrong calling date() three times in one line. I'm not a developer by trade.
$timestamp = date('m-d-Y')." ".date('h:i A', strtotime(date('H:i:s')));
which results in: 05-28-2014 03:49 PM
Thoughts?
When you need the current timestamp, you can use,
$timestamp=date("m-d-Y h:i A");
When you need to format the timestamp you fetched from database or other means, you have to use strtotime.
$format_timestamp=date("Y-m-d H:i:s", strtotime($timestamp)); // I just convert your format to YYYY-MM-DD HH:MM:SS format.
Edit:
When you need to subtract x hours from the current time, use
$timestamp=date("m-d-Y h:i A", strtotime("-4 hour"));
Some more examples,
$timestamp=date("m-d-Y h:i A", strtotime("+2 hour")); // Adds 2 hours
$timestamp=date("m-d-Y h:i A", strtotime("+1 day")); // Adds 1 Day
You can simplify making:
$timestamp = date('m-d-Y h:i A');
or
$timestamp = gmdate("M d Y H:i:s");
For more information, see:
PHP Manual
date http://www.php.net/manual/en/function.date.php
gmdate http://www.php.net/manual/en/function.gmdate.php