Is it possible to add text between date and time in PHP ?
<?php
echo date();
?>
This will create
(07-06-2014 00:00)
But i want (07-06-2014 at 00:00 hours).
Derived from your example which you provided a bit late
echo date('d-m-Y \a\t H:i:s') . ' hours';
be sure to use the exact syntax given! Using double quotes "
instead of single ones '
will result in you getting a tab for \t
, you'd than need to use \\a\\t
for proper syntax as in the example below:
echo date("d-m-Y \\a\\t H:i:s") . ' hours';
This is due to how php quoting works and has nothing to do with date formatting, things in '
don't get escaped while those in "
do, so if you use "
be sure to use double backslashes \\
.
If it's a datetime object
echo $datetime->format('d-m-Y \a\t H:i:s') . ' hours';
if you already have it as a string
echo str_replace(' ', ' at ', $datetime) . ' hours';
yes you can.
$date=date("m-d-Y");
$time=date("H:i:s");
$display=$date.'at'.$time;
\a\t didn't work for me, instead \\a\\t
works very well
date("d-m-Y \\a\\t H:i");
Strangely enough I wanted to put
date("l the jS F Y");
to read e.g. Monday the 9th January 2017 but instead I got "Monday 3106UTC 9th January 2017". I read the answer above from xception and changed this to date("l \t\h\e jS F Y")
, which gave me "Monday h 9th January 2017", so I read the comment from rybo111 and changed this to date("l \t\\h\\e jS F Y")
and it still didn't work as I now got "Monday he 9th January 2017".
After a bit of head scratching I worked out that if letters in the word also form part of the date() function, for example t = Number of days in the given month, these need to be double escaped, otherwise a single escape will suffice. Hence why "at" needs to be coded \a\\t
and "the" as \\t\\h\\e
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With