This is weird, i'm trying to echo out a date like Monday, January 1, 2013, but its echoing out the wrong textual day. I don't have a clue why?
I have:
<?php echo date('l, F n, Y', strtotime($do['dueDate'])); ?>
And $do['dueDate'] is the date from the database of "2013-03-22". Its formatted as DATE in mysql.
When the above echos out it says: Friday, March 3, 2013
But march 3, 2013 is a sunday...
Try like this
echo date('l ,F j ,Y', strtotime($do['dueDate']));
Try this :
n --> Numeric representation of a month, without leading zeros --> 1 through 12
j --> Day of the month without leading zeros --> 1 to 31
<?php
$do['dueDate'] = "2013-03-22";
echo date('l, F j, Y', strtotime($do['dueDate']));
?>
Output :
Friday, March 22, 2013
use this
<?php
echo date('l, F j, Y', strtotime("2013-03-22")); // output Friday, March 22, 2013
working example http://codepad.viper-7.com/rF6w1U
http://php.net/manual/en/function.date.php
n is the numerical representation of the month.
try this:
$date = "2013-03-22";
echo date('l, F j, Y', strtotime($date));
outputs:
Friday, March 22, 2013
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
The code I am currently using displays the date like Thursday, June 13, 2019.
Current Code: <?php echo date("l, F j, Y", strtotime(date('d-m-Y'))); ?>
How can I display the date like this instead? Thur, June 13, 2019?
Thanks
Thanks to Alex, the correct answer would be <?php echo date("D, F j, Y", strtotime(date('d-m-Y'))); ?>
<?php echo date("D, F j, Y", strtotime(date('d-m-Y'))); ?>
OutPut : Thu, June 13, 2019
More About Php Date Function: [https://www.w3schools.com/php/func_date_date.asp][1]
I am saving date and time in a single column i.e 2015-04-15 13:22:58 and I want to show this time and date in this format: 15th April 2015, 1:22 pm.
I have used this code:
$dt = '2015-04-15 13:22:58';
echo date("jS F Y", strtotime($dt));
and it shows only date (15th April 2015). How can I fetch time along with date?? Any help would be highly appreciable.
Try
$dt = '2015-04-15 13:22:58';
echo date("jS F Y h:i a", strtotime($dt));
//Result is 15th April 2015 01:22 pm
echo date("jS F Y g:i a", strtotime($dt));
// Result is 15th April 2015 1:22 pm
change "jS F Y" to "js F Y g:i a". That will output the time in your desired format as well as the date.
$dt = '2015-04-15 13:22:58';
echo date("jS F Y g:i a", strtotime($dt));
Output:
15th April 2015 1:22 pm
if you want to format date time in php, you can use date_format like this.
echo date_format(strtotime($dt), "jS F Y g:i A");
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");
?>
How to find month no,name from week number using php
If you have the ISO week number, then to get the month (of the start of the week) you can use strtotime like:
// F = full name of month, n = month number without leading zero
echo date('F n', strtotime('2010-W50'));
Bear in mind that the ISO week might not be the same as your meaning of week, so read on.
If you want to count the whole weeks since January 1st of this year (regardless of what day of the week that is) then you could do as Adnan mentioned:
echo date('F n', strtotime('1 Jan + 50 weeks'));
echo date('F',strtotime('1 January 2010 +50 weeks'));
www.php.net/date
www.php.net/strtotime
Have a look at php date() - http://php.net/manual/en/function.date.php
Here are some good examples:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
$myDate = "2010-05-12";
$weekNumber = date("W", strtotime($myDate));
Just replace the "W" with the value you need. Full reference:
http://php.net/manual/en/function.date.php
If you have a week number, and want the date from it you can use:
date("d m Y", strtotime("1.1.2010 + 30 weeks"));