I run this:
echo date('l, F jS Y','2011-02-12 14:44:00');
I get this:
Wednesday, December 31st 1969
What's wrong?
The second argument for date needs to be a UNIX timestamp, not a date string. Use:
echo date('l, F jS Y', strtotime('2011-02-12 14:44:00'));
http://php.net/manual/en/function.date.php
Related
I'm building a CMS system using PHP for the first time. Part of this project involves me printing out a date from a column in my database using a loop. This is what the loop currently looks like:
<td><?php echo $record['fromDate']; ?> - <?php echo $record['toDate']; ?> | <?php echo $record['location']; ?></td>
The date is printing out like this: 2022-03-03 - 2022-03-23
What can I do to print it out as follows: 03 March 2022 - 23 March 2022
To begin, this can be interpreted as PHP problem rather than SQL
You can use date_format() PHP function and format it with 'd F Y'while displaying with HTML
date_format(date_create('2000-01-01'), 'd F Y')
// 01 January 2000
As per docs
F - A full textual representation of a month, such as January or March
so your code would look like
<?php echo date_format(date_create($record['fromDate']), 'd F Y'); ?> - <?php echo date_format(date_create($record['toDate']), 'd F Y'); ?>
Word of Warning: If you're not telling your program exactly how to parse a date, it's guessing.
At some point you're going to feed a date like 2022-03-04 into it and get a result that you're not expecting.
$date_str = '2022-03-03';
$dt = DateTime::createFromFormat('Y-m-d', $date_str)->format('d F Y');
var_dump($dt);
Output:
string(13) "03 March 2022"
As seen in the accepted answer to this similar post:
$originalDate = $record['fromDate']; //or $record['toDate']
$newDate = date("j F Y", strtotime($originalDate));
And use $newDate in your echo statement.
j is the day of the month (without leading zeros), F is the month name, and Y is the year.
date() php
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.
I would like to display this format of the current date using PHP. I have googled and found a few variances of what I want to do but the company I work for wants it specifically in this format to match what they've mailed out.
Monday, March 16, 2015
Thanks.
If you looked at the PHP documentation for date you'd be able to figure it out very easily:
echo date('l, F j, Y');
which outputs
Monday, March 16, 2015
As mentioned by #alfallouji, you should have a look at the php documentation.
Here is what you need:
$now = new \DateTime();
echo $now->format('l, F j, Y');
The date format string should be "l, F d, Y".
You can display current date in this format as below:
<?php
echo date("l, F d, Y");
?>
i have a problem with news script.
the code below will generate today's date, i think it's because that i have date() in the beggining, or maybe the line is just not written right.
the $postdate in the () is a numeric date, such as 13.11.01, again, i get the same date of today on all of the posts in the loop.
what's wrong with my line?
i need it to output the date as text one. such as December 13th, 2013
thanks
<?
$postdate = date('F jS, Y', strtotime($postdate));
?>
The problem is likely that strtotime() doesn't recognize the . character as a valid separator. See below:
Strtotime() doesn't work with dd/mm/YYYY format
First you have a typo on your question.
13.11.01 is November 1st, 2013 , Not November 13th, 2013
So, go the OOPway like this.
<?php
$dt='13.11.01';
$date = DateTime::createFromFormat('y.m.d', $dt);
echo $datex= $date->format('F jS, Y'); //"prints" November 1st, 2013
I have date in format 2011-01-28 06:34:33 i.e. date("Y-m-d H:i:s"). I want to convert it into 28th January 2011.
How can I change it?
Supply the date function with your format, which can be found here. Pass the timestamp of your original date as the second parameter to date. You can obtain the timestamp by using strtotime.
date("dS F Y", strtotime("2011-01-28 06:34:33"));
Use
$dateStr = date("jS F Y", time());
The day value is without leading zero.
Try this echo date('jS F Y h:i:s A');