Output time and date with PHP - php

I'm having problems trying to get the time to display with php. I can get the date to work but not the time. The format it has to be in is 2:46PM on January 29 2017.
This is what I have to far.
echo date(' F j Y');

As simple as this
echo date("g:i a, F j, Y ");

According to PHP documentation http://php.net/manual/en/function.date.php
echo date('g:i A F d Y');

Related

Printing dates as strings in SQL

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

How to Convert Date and Time String into Different format?

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

Display todays day of the week, month, day, year in THIS format in PHP?

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

PHP Date - How to add a string to separate date and time

I want to display date and time format something like this "May 23 at 12:30pm".
I saw in PHP manual and found:
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
After modification I manage to get
echo date('M j \of h:i a');
it is giving me "May 23 of 12:30pm"
but when i replacing of with at it is giving me "May 23 a23 08:26 pm".
I don't what is going wrong.
you need to escape the a and t as both have special meaning when used as formatting options in date()
echo date('M j \a\t h:i a');
See it in action
Try
<?php
echo date('M j \a\t h:i a');
?>
OR
<?php
echo date('M j'). "at". date(' h:i a');
?>
You need to escape the t too:
echo date('M j \a\t h:i a');
Another option could be:
echo date('M j')." at ".date('h:i a');
You need to try this in php:
$t = now(); //This will give you current time
echo date_format($t,"dS M,y \a\\t h:i a"); //14th May,19 at 05:39am

PHP date conversion dd [th/st/rd] / month / yyyy

I have my users entering the date in this format :- mm/dd/yyyy (11/21/2012)
My PHP script converts the date into the following format :- dd-Month-yyyy (21-November-2012)
I do this using :-
$new_date = date('d-F-Y', strtotime($user_date));
How can I have the date in this format :- 21st November 2012?
Thanks
You can use S letter as following:
$new_date = date('jS F Y', strtotime($user_date));
Check manual.
It will output as you expect
$my_date = '2016-01-01';
echo date('F jS, Y', strtotime($my_date));
# January 1st, 2016
while dS will also prepends 0
echo date('F dS, Y', strtotime($my_date));
# January 01st, 2016
$new_date = date('jS F Y', strtotime($date));
S - English ordinal suffix for the day of the month, 2 characters
(st, nd, rd or th. Works well with j)
**My Date = 22-12-1992**
<?php
$mydate = "22-12-1992";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
**OutPut = 22nd December 1992**
You can use something like:
echo date('l, F jS');
Or even get a bit fancy with the HTML:
echo date('l, F j<\s\u\p>S</\s\u\p>');
You can do that :
<?php echo date('dS M. Y');?>
$date = date_create('09-22-2012');
echo $date->format('d S F Y');
by this code you will get your desire
for more you can also visit http://php.net/manual/en/datetime.formats.date.php

Categories