i'm compiling a church register and the dates are in this format yyyy-mm-dd
example. 1978-03-27 .I need to make it 27th March,1978. Any php script to help me out?
I tried MSEXcel but messes it up. thanks (anyway..i'm getting it from a database) so i would be glad if i have it been read from a database or a file of dates.
Try with:
$input = '1978-03-27';
$timestamp = strtotime($input);
$output = date('dS F, Y', $timestamp);
Reference to date format: php.net
Try this:
$in = '1978-03-27';
$out = date('jS F Y',strtotime($in));
var_dump($out);
date() formats your output, and strtotime() converts your input to a timestamp. Keep in mind this will only work for values from 1970-01-01. If you want to work with "older" dates, you have to parse the input manually and generate the output you require.
Related
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");
I have gone through convert php date to mysql format but still I have few issues and questions,
In general, my application, can input dates in Y-m-d, d-m-y, m-d-Y, jS, F Y format, or even with '/' separator.
I want this dates to be converted in to MYSQL Y-m-d format,
I tried as below, but it shows different output than expected,
Non working
date_format(date_create_from_format('d-m-Y', '02-25-2016'), 'Y-m-d');
Working
date_format(date_create_from_format('d-m-Y', '25-02-2016'), 'Y-m-d');
So it seems format and string to be match in same way, otherwise its not interpreting correctly,
What is best way to convert above 4 format inputs to mysql(Y-m-d) format?
Thanks advanced,
I'd recommend using Carbon for any date processing in PHP these days.
Creating date by parsing strings is simple enough...
http://carbon.nesbot.com/docs/#api-instantiation
Getting the output in a format you need is also simple...
http://carbon.nesbot.com/docs/#api-formatting
I'd suggest you to use this function: DateTime::createFromFormat
Using this, you need to create a date by specifying the particular format.
Try this:
$date = DateTime::createFromFormat('Y-m-d', '2016-05-26');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('d-m-Y', '26-05-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('m-d-Y', '05-26-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('jS F Y', '26th May 2016');
echo $date->format('Y-m-d');
Thanks for all suggestions, i think i found my logic,
My date format selection is stored in sql db, so will try as below,
date_format(date_create_from_format('format from db', 'user entered format'), 'Y-m-d');
As user entered format is stored in db, so i will pull that in this date_format function, so both will match and i can convert to Y-m-d!
Thanks,
I am working on Google Calendar API using its PHP library and I need to set a recurring event for which I have to create a RRule string which should be of following format:
RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z
I am unable to create the date in above format. I literally tried all methods like:
date('YmdHis');
date(DATE_RFC2822);
date('c');
But Google doesn't accept any of above formats. I need to make it like: 20110701T170000Z. Can anybody help me on this?
Thanks
This is your date format:
date('Ymd\THis\Z')
becomes
20150429T154315Z
Look at the examples here: http://php.net/manual/de/function.date.php
Are you able to concatenate two strings together and form the required datetime string in RFC2822?
<?php
$ymd = date('Ymd');
$hms = date('His');
echo $ymd."T".$hms."Z"; //Will output YYYYMMDDTHHMMSSZ
?>
Let me know if this works! :)
Try this:
date('Ymd\THis\Z', $timestamp);
or
date('Ymd\THis\Z');
if you want the current timestamp
I am using an html form to get a user inputted date. The structure of the date inputted is: MM/DD/YYYY. I then need to increment the total days by 196 in PHP. Right now, the data is being posted to a php file called Calculate.php. I was looking into altering the data using date (m d Y); in php, but my friend said that probably wont work. Any ideas? Thank you for your time and have a great day! ^_^
Check out strtotime. This will convert your MM/DD/YYYY format in to a numeric value you can then work with.
Use strtotime again to manipulate the date to add the days to it.
Use strftime to re-format it for display.
e.g.
$d = '08/11/2011';
$dAsPOSIX = strtotime($d);
$dPlus196Days = strtotime('+196 day', $dAsPOSIX);
echo strftime('%m/%d/%Y',$dPlus196Days);
DEMO
Using strtotime magic:
strtotime('08/11/2011 +196 days');
$input = $_POST['date'];
echo date('m d Y', strtotime('+196 days', strtotime($input));
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;
}
}