Add text between date and time - PHP

前端 未结 5 1502
我在风中等你
我在风中等你 2020-12-09 18:07

Is it possible to add text between date and time in PHP ?



This will create (07-06-2014 00:00)

相关标签:
5条回答
  • 2020-12-09 18:30

    First please put the given date in ISO Format so that strtotime() can read it and then use date() function to format the date accordingly.

    date('/*DATE FORMAT YOU WANTED*/', strtotime('/*ISO FORMATTED DATE*/'));
    
    0 讨论(0)
  • 2020-12-09 18:33

    yes you can.

    $date=date("m-d-Y");
    $time=date("H:i:s");
    $display=$date.'at'.$time;
    
    0 讨论(0)
  • 2020-12-09 18:34

    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';
    
    0 讨论(0)
  • 2020-12-09 18:39

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

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

    0 讨论(0)
  • 2020-12-09 18:45

    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.

    0 讨论(0)
提交回复
热议问题