Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text between date and time - PHP

Tags:

php

datetime

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).

like image 725
FrancisMV123 Avatar asked Oct 12 '12 11:10

FrancisMV123


4 Answers

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';
like image 91
xception Avatar answered Oct 01 '22 09:10

xception


yes you can.

$date=date("m-d-Y");
$time=date("H:i:s");
$display=$date.'at'.$time;
like image 29
Manoj Avatar answered Oct 01 '22 11:10

Manoj


\a\t didn't work for me, instead \\a\\t works very well

date("d-m-Y \\a\\t H:i");

like image 32
Rand Avatar answered Oct 01 '22 09:10

Rand


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.

like image 27
luke_mclachlan Avatar answered Oct 01 '22 11:10

luke_mclachlan