0

Hi I am new into PHP and I really really like it so var. I have a string which I want to convert into a custom date and time by adding just simple characters. I first tried fixing it by myself, but everything I tried didn't worked for me.

Input:

$customdate = "27032017042100";

I want to add to the string above characters like: / and :

Expected result:

27/03/2017 04:21:00

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Berk Balik
  • 91
  • 8
  • Why don't you simply take a look into the php documentation to learn about the date functions? http://php.net/manual/en/refs.calendar.php – arkascha Mar 27 '17 at 09:47
  • Welcome to Stack Overflow! Please [edit] your question to show [what you have tried so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Mar 27 '17 at 09:55

4 Answers4

0

This is a simple date function:

echo date('d/m/Y h:i:s');

See http://php.net/manual/es/function.date.php for more ideas.

mayersdesign
  • 5,062
  • 4
  • 35
  • 47
0

Its quite simple.

<?php
    $customdate = "27032017042100";
    echo gmdate("Y-m-d\TH:i:s\Z", $customdate);
?>

Here is the previous answered question: Converting a UNIX Timestamp to Formatted Date String

Community
  • 1
  • 1
Geee
  • 2,217
  • 15
  • 30
0

If you need to convert your data from STRING, you can use folowing code

if (preg_match("/([0-9]{2})([0-9]{2})([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})/", $customdate, $rg))
    $newdate = $rg[1]."/".$rg[2]."/".$rg[3]." ".$rg[4].":".$rg[5].":".$rg[6]
diavolic
  • 722
  • 1
  • 4
  • 5
0

Changing date format from one to another.

PHP code demo

$date = date_create_from_format('dmYHis', '27032017042100');
echo date_format($date, 'd/m/Y H:i:s');
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42