I'm working with an XML document that is returning variables and for some reason in a xml return the timestamp is formatted like this... 20180606T110000 ... why anyone would format it like that makes no sense to me; however, its what I have to work with. ITs formatted YYYYMMDD , the T is the split between date and time, HHMMSS. ITs set up in a 24 Hour clock that I also need to convert to 12 hr clock with am/pm
I need that formatted like 06/06/2018 11:00:00 AM.
Is there a way to do that via a date format (I know how to use date() but I don't know how to bring in that timestamp the way its formatted) or even separating it out into
$year = xxxx
$month = xx
$day = $xx
$Hour=xx
etc. etc. etc.
if need be.
I've briefly looked at php's date create from format ( date_create_from_format('j-M-Y', '15-Feb-2009') ) but dont fully understand how that works.
I've also thought about a split. I've also looked at chunk_split and wordwrap but its not even amounts of characters so that would be complex to create.
Any ideas?
The format you're working with is "XMLRPC (Compact)" format. This is fully supported by PHP (you can see a list of supported formats here). To get what you want, just use a combination of strtotime() and date().
$timestring = "20180606T110000";
$timestamp = strtotime($timestring);
echo date("m/d/Y h:i:s A", $timestamp);
You can use PHP DateTime to parse a datetime String with any format. Please view the Parameters format in the following link to understand how the "Ymd\THis" part works: http://php.net/manual/en/datetime.createfromformat.php
<?php
$time = "20180606T110000";
$date = DateTime::createFromFormat("Ymd\THis", $time);
// 06/06/2018 11:00:00 AM.
echo $date->format("d/m/Y h:i:s A");
Related
I have a php string from db it is 20/11/2017 I want to convert it milliseconds.
It's my code to doing that.
$the_date = "20/11/2017";
$mill_sec_date = strtotime($the_date);
var_dump($mill_sec_date);
But it does not print any thing rather than
bool(false);
What is the problem and how can i solve it ????
When using slashes to separate parts of the date, PHP recognizes the format as MM/DD/YYYY. Which makes your date invalid because there is no 20th month. If you want to use the format where day and month is swapped, you need to use hyphens, like DD-MM-YYYY.
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
print_r($newformat);
Use DateTime class to call function createFromFormat
$date = date_create_from_format('d/M/Y:H:i:s', $string);
$date->getTimestamp();
Most likely you got the date format wrong, see
here for a list of supported date and time formats:
This section describes all the different formats that the strtotime(), DateTime and date_create() parser understands.
You string is not accept by the strtotime, you can use createFromFormat set set the with the format type of the time string like below, you can also check the live demo. And you also can refer to this answer
var_dump(DateTime::createFromFormat('d/m/Y', "20/11/2017"));
I am new to PHP and I am trying to learn more of php date and time but I seem to get stuck with this.
I have this date format:
ddMMyyHHmmss
And an example is 120813125055 but I am trying to manipulate the string such that it will give me the format of:
yyyy-MM-dd HH:mm:ss (in the example above, 2013-08-12 12:50:55)
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
But it always gives me a result of 1969-12-31 18:00:00.
I assume that I need to do some string manipulation in PHP for this but I was wondering if there is an easier and more efficient way to do it?
I think what you're looking for is in the second response answered here: how to re-format datetime string in php?
To summarize (and apply to your example), you could modify the code like this.
$datetime = "120813125055";
$d = DateTime::createFromFormat("dmyHis", $datetime);
echo $d->format("Y-m-d H:i:s");
Use date_create_from_format:
$ts = date_create_from_format('dmyHis', '120813125055');
$str = date('Y-m-d H:i:s', $ts);
strtotime() only works on EASILY recognizable formats. Your is a ugly mix of garbage, so no surprise that strtotime bails with a boolean FALSE for failure, which then gets typecast to an int 0 when you tried feed it back into date().
And of course, note that your time string is NOT y2k compliant. two digit years should never ever be used anymore, except for display purposes.
You're using your function call and the argument the wrong way around.
In your example, php will try to return you the date for which the time is 'strtotime('120813125055')', and this function returns false (interpreted as 0). So you get returned the date formatted in 'Y-m-d H:i:s' for the Unix epoch.
You will need to get the actual timestamp of your string, so use http://www.php.net/manual/en/datetime.createfromformat.php.
You are mistaken here..
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
You shouldn't use only numbers ( doesnt matter its an integer or a string ), than it will always give you the same thing.
You can use any other valid date and time ( E.G. 6 Jun 2013, 5 may 12...) . Because what strtotime() do is detect a valid date and convert it into timestamp.
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');
To change 2009-12-09 13:32:15 to 09/12/2009
here:
echo date("d/m/Y", strtotime('2009-12-09 13:32:15'))
You can use strtotime to get the timestamp of the first date, and date to convert it to a string using the format you want.
$timestamp = strtotime('2009-12-09 13:32:15');
echo date('d/m/Y', $timestamp);
And you'll get :
09/12/2009
[edit 2012-05-19] Note that strtotime() suffers a couple of possibly important limitations:
The format of the date must be YYYY-MM-DD; it might work in some other cases, but not always !
Also, working with UNIX Timestamps, as done with date() and strtotime() means you'll only be able to work with dates between 1970 and 2038 (possibly a wider range, depending on your system -- but not and illimited one anyway)
Working with the DateTime class is often a far better alternative:
You can use either DateTime::__construct() or DateTime::createFromFormat() to create a DateTime object -- the second one is only available with PHP >= 5.3, but allows you to specify the date's format, which can prove useful,
And you can use the DateTime::format() method to convert that object to any date format you might want to work with.
Using the date() method.
print date("d/m/Y", strtotime("2009-12-09 13:32:15"));
$long_date = '2009-12-09 13:32:15';
$epoch_date = strtotime($long_date);
$short_date = date('m/d/Y', $epoch_date);
The above is not the shortest way of doing it, but having the long date as an epoch timestamp ensures that you can reuse the original long date to get other date format outputs, like if you wanted to go back and have just the time somewhere else.
Duplicate
Managing date formats differences between PHP and MySQL
PHP/MySQL: Convert from YYYY-MM-DD to DD Month, YYYY?
Format DATETIME column using PHP after printing
date formatting in php
Dear All,
I have a PHP page where i wil be displaying some data from Mysql db.
I have 2 dates to display on this page.In my db table, Date 1 is in the format d/m/Y (ex: 11/11/2002) and Date 2 is in the format d-m-Y (ex : 11-11-2002)
I need to display both of this in the same format .The format i have stored in a variable $dateFormat='m/d/Y'
Can any one guide me
Thanks in advance
Use strtotime to convert the strings into a Unix timestamp, then use the date function to generate the correct output format.
Since you're using the UK date format "d/m/Y", and strtotime expects a US format, you need to convert it slighly differently:
$date1 = "28/04/2009";
$date2 = "28-04-2009";
function ukStrToTime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}
$date1 = date($dateFormat, ukStrToTime($date1));
$date2 = date($dateFormat, ukStrToTime($date2));
You should be all set with this:
echo date($dateFormat, strtotime($date1));
echo date($dateFormat, strtotime($date2));
You may want to look into the strptime function. This can convert any date from a string back into numeric values. Unlike strtotime, it can be adapted to different formats, including those from different locales, and its output is not a UNIX timestamp, so it's capable of parsing dates before 1970 and after 2037. It may be a little bit more work though because it returns an associative array though.
Unfortunately it's not available on Windows systems either so it's not portable.
If for some reason strtotime will not work for you, could always just replace the offending punctuation with str_replace.
function dateFormat($date) {
$newDate = str_replace(/, -, $date);
echo $newDate;
}
echo dateFormat($date1);
echo dateFormat($date2);
I know this will make most folks cringe, but it may help you with formatting non-date strings in the future.
rookie i am. so came up with the method that just do that. what mysql needs.. shish i used param 2... hope it helps. regards
public function dateConvert($date,$param){
if($param==1){
list($day,$month,$year)=split('[/.-]',$date);
$date="$year-$month-$day"; //changed this line
return $date;
}
if ($param == 2){ //output conversion
list($day,$month,$year) = split('[/.]', $date);
$date = "$year-$day-$month";
return $date;
}
}