Calculate a date with PHP - php

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

Related

separating a weird formatted timestamp into an understandable date

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");

php convert date to mysql format

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,

Convert SQL Time using PHP?

I want to convert MySQL Time data type using PHP and Javascript. I know it can be done using FORMAT_TIME of MySql, but I would like to do the same with php. The Time format is hh:mm:ss by default and I would like to convert it to hh:mm.
Just feed the date into the following function(s):
<?php echo date("h:i", strtotime($date)); ?>
//any date is ok, we care only about the hours and minutes
// $your_date is what comes from db in format hh:mm:ss
$date = new DateTime('2000-01-01 '.$your_date);
echo $date->format('H:i');
I think you could create a DateTime object and then use format("h:i") to get the desired output

php/mysql getting date formats

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.

Date_format() ? Question help

If I have a MySQL table field of my one date with this format (09-13-2011 11:22:43), as I do for PHP to print with this format (09/13/2011 11:22:43) Sorry my ignorance I searched the php.net site but can not find something about my request, Apologies.
$mysql_date = '09-13-2011 11:22:43';
$date = DateTime::createFromFormat('m-d-Y H:i:s', $mysql_date);
echo $date->format('m/d/Y H:i:s');
Use:
date( "m/d/Y h:i:s", UNIX_TIMESTAMP($fieldname));
If your field is already timestamp use the following:
date( "m/d/Y h:i:s", $fieldname);
I may be wrong in saying this but I don't think theres a standard way in doing so, unless you want to save the date/time as a unix_timestamp. If you do then you can format the time in however you want using the php date() function. If you aren't then you can always use something like str_replace() on the times to get them to the format you want or even use regex if your feeling adventurous
MySQL's DATE columns format is fairly irrelevant. Just use DATE_FORMAT() to convert the date to a string that suits your needs.

Categories