Why does it display the wrong date in php - php

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.

Related

How to make Php Timestamp Function For My zone

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));
?>

date conversion in CodeIgniter / PHP does not gives output

I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>

Today's date format - Day (Shorthand) Date Number Month (Shorthand) - PHP

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

DateTime formate in php

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");

strtotime() Is Adding An Extra Day

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.

Categories