I have a jquery function which passes the date '27/05/2016 11:25 PM' to a PHP file. The PHP file will update this to the database.
I'm using strtotime to convert this string to date format '05/27/2016 11:25 PM' but strtotime returns false.
My PHP date conversion:
$EndDate = strtotime($Date);
$NewEndDateValue = date('m/d/Y h:i A', $EndDate);
var_dump($NewEndDateValue); //this returns false
strtotime() by default treats dates with / seperators as the wierd USA format for dates, where they start in the middle of a date and work outwards from there (go figure). Its fine to speak it that way but totally illogical to expect a logic machine (computer) to work that way.
Anyway, all you need to do is convert the / to a - and date() will assume a logical date format and therefore work.
<?php
$Date = '27/05/2016 11:25 PM';
$dat = str_replace('/', '-', $Date);
$EndDate = strtotime($dat);
$NewEndDateValue = date('m/d/Y h:i A', $EndDate);
var_dump($NewEndDateValue); // "05/27/2016 11:25 PM"
Related
In my CodeIgniter application, I am getting date in different formats, such as: April 1st 2017, May 29, 2015, Jun-15-2015, 10-September-2015 and sometimes even with extra string such as Start: April 1, 2017. However, I want to convert the input date from any format to Y-m-d in order to save it in MySQL database. For example, if input date is April 1st 2017 I should get 2017-04-01. I have used below posted code for that but it is not working for all of the above mentioned cases. So please tell how can I write general conversion logic that can convert date from any format even if date has extra string with it (as mentioned above) to Y-m-d format.
Code:
$date = DateTime::createFromFormat('F jS Y', $old_date);
$new_date = $date->format('Y-m-d');
try this
$old_date = 'Jun-15-2015';
echo $newDate = date("Y-m-d", strtotime($old_date));
You can replace the extra string like
$date = str_replace('Start: ',' ',$date);
And after you can use date function of php
echo $date = date('Y-m-d',strtotime($date));
This might help even for inserting this date format into database tables
function convertUTCCombinedToLocal($utcDateTime) {
$utcDateTime = explode(" ", $utcDateTime);
$date = explode("-", $utcDateTime[0]);
$time = explode(":", $utcDateTime[1]);
$localDate = new DateTime();
$localDate->setTimezone(new DateTimeZone('UTC'));
$localDate->setDate($date[0], $date[1], $date[2]);
if (count($time) == 3) {
$localDate->setTime($time[0], $time[1], $time[2]);
} else {
$localDate->setTime($time[0], $time[1], '00');
}
$localDate->setTimezone(new DateTimeZone('Asia/Kolkata'));
return $localDate->format('d/m/Y H:i:s');
}
I'm using datetimepicker for user input. The format is: mm-dd-yy hh:mm tt (Example: 08-25-2014 01:49 pm). I need to convert it to MySQL datetime format using php (Example of same time properly converted 2014-08-25 13:49:00) for MySQL storage.
<?php
$test = '08-25-2014 01:49 pm';
$test = str_replace("-","/","$test");
$test = new DateTime("$test");
$test = date_format($test, 'Y-m-d H:i:s'); // 2011-07-01 00:00:00
?>
Update: For some reason I had to remove the - and replace it with /. Any idea why forward slashes are fine but not -?
As you are using an American date format and the - seperator which is used for NON US date formats, you will probably have to actually do something like this :-
$date = '08-25-2014 01:49 pm';
$dt = new DateTime();
$dt->createFromFormat('m-d-Y h:i a', $date);
$date = $dt->format('y-m-d');
You can do this using the DateTime class:
$mydate = new DateTime("08-25-2014 01:49 pm");
$MySQL_format = $mydate->format("m-d-Y h:i a");
Given a date string formatted Y-m-d (2014-4-11 for example), how can I get a UNIX timestamp of 12am the beginning of that day? Would it involve date_parse_from_format()?
Thanks
You can simply use strtotime()
$date = "2014-04-11";
$timestamp = strtotime($date);
which inturm gives you -
$d = date('Y-m-d H:i:s', $timestamp); // 2014-04-11 00:00:00
Try online conversion to test - http://www.onlineconversion.com/unix_time.htm
I have the following timeformat:15/08/2011 12:32:23 day/month/year hour:minute:sec and I want to convert to the following format: Y-m-d H:i:s
I tried with date('Y-m-d H:i:s', strtotime($time)) but it not works. It swaps the month and the day when it's converting from string to datenum.
strtotime understands both American (mm/dd/YYYY) and European (dd-mm-YYYY or dd.mm.YYYY) formats. You are using slashes to separate day, month and year, and that's why your date is interpreted as American. To solve that, replace the slashes with dashes.
In which case, you could very simply swap the month and date from your string:
$time_string = "15/08/2011 12:32:23";
$strtotime = explode("/",$time_string);
$strtotime = implode("/",array($strtotime[1], $strtotime[0], $strtotime[2]));
echo date('Y-m-d H:i:s', strtotime($strtotime));
Working Example: http://codepad.viper-7.com/234toO
You are going to have to reparse the time. strtotime is thinking you are trying to input a string in the format of 'm/d/Y H:i:s', but you are supplying 'd/m/Y H:i:s'.
list($date, $times) = explode(' ', $time);
list($day, $month, $year) = explode('/', $date);
$newTime = date('Y-m-d H:i:s', strtotime("$month/$day/$year $times");
Replace slashes with hyphen in the date then try it will work.
$a = '07/08/2019'; // 07 is day 08 is month.
echo date('Y-m-d', strtotime($a)); //output: 2019-07-08 where 07 became month.
$a = str_replace("/","-","07/08/2019"); // 07-08-2019
echo date('Y-m-d', strtotime($a)); //2019-08-07
I am pulling the dates of various posts from a database. The dates are in the following format:
2009-08-12
Numeric Year - Numeric Month - Numeric Day
How can I reformat these dates to something more user friendly like:
August 12, 2009
Numeric Month Numeric Date, Numeric Year
Assuming that the date gotten from the mysql database is stored in a variable called:
$date = $row['date_selected'];
Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.
$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));