This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
Hi I am trying to get some PHP date formatting resolved. The problem I am having is random unexpected results being returned instead of the correct date after I try and format them.
I have the date MM/DD/YY passed in as a string and I am wondering how to convert that to a YYYY/MM/DD. When it try to convert the date it is like it is staying in the same place but trying to convert the individual section so the MM (12) goes to the year YYYY (2012)
Here is the section of code I have been using to try and change the format:
$date = $_GET["datepicker"];
$arr[$i] = strftime("%Y-%m-%d", strtotime($date));
The $arr[$i] is just the array I am putting it into this shouldn't affect anything.
I have also tried the following:
$arr[$i] = date("Y-m-d", strtotime($date));
Thanks,
Kieran
The easiest way to get the output you need is to use the DateTime class, in particular: DateTime::createFromFormat:
$date = DateTime::createFromFormat('m/j/y', $_GET['datepicker']);
//now to get the outpu:
$arr[$i] = $date->format('Y-m-d');
There is a procedural-style alternative, too:
$date = date_create_from_format('m/j/y', $_GET['datepicker']);
//note, date_create_from_format returns an instance of DateTime
$arr[$i] = date_format($date, 'Y-m-d');//same as $date->format('Y-m-d');
You can use mktime:
$date = $_GET["datepicker"];
list($month, $day, $year) = explode('/', $date);
$timestamp = mktime(0, 0, 0, $month, $day, $year);
$output = date('Y-m-d', $timestamp)
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');
}
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have date in this format 'm-d-Y' (12-31-2017). I want to convert it into a timestamp. Normally, this works:
$timestamp = strtotime($date);
But if I am not mistaken, that works with 'd/m/Y' and not 'm/d/Y'.
What's the solution here?
You can use DateTime::createFromFormat() and then call getTimestamp() on the Object:
//first create DateTime Object
$datetime = DateTime::createFromFormat('m-d-Y', '12-31-2017');
//get timestamp from DateTime
echo $datetime->getTimestamp();
An another solution is the following:
//CONVERT the time string from the desired format
$dateTime=DateTime::createFromFormat('m/d/Y',$date);
echo "UNIX TIMESTAMP method 1".$date->format("U");
// false
echo "UNIX TIMESTAMP method 2".$date->getTimestamp();
You can change date according your format :
$date = '12/31/2017';
$timestamp = date('Y-m-d', strtotime($date));
$timestamp = date('d-m-Y', strtotime($date));
$timestamp = date('m-d-Y', strtotime($date));
if Timestamp
$timestamp = date('m-d-Y h:i:s', strtotime($date));
Try this code .
<?php
$d=strtotime("12/31/2017");
echo "Created date is " . date("m-d-Y h:i:sa", $d);
?>
Output
for more Information about date time format then read PHP Manual
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
So far I'm doing this to convert dates
$date = '01/1990';
date('Y-m-d', strtotime(str_replace('-', '/', $date . '/01')));
But I get back a boolean false; Any ideas?
Your str_replace is searching for - and replacing with /.
Here is a simplified solution that opts to not concatenate the day onto the date string.
$date = '01/1990';
$date = str_replace('/', '-', $date);
$date = date('Y-m-1', strtotime($date));
If you do want to concatenate your day onto the date string format as dd/mm/yyyy
$date = '01/1990';
$date = '01/' . $date;
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
Replace $data with $date:
echo date('Y-m-d', strtotime(date('Y-d-m', strtotime('01/' . str_replace('-', '/', $date)))));
strtotime accepts dd-mm-yyyy and mm/dd/yyyy (and some other formats like yyyy-mm-dd). The only way to differentiate between dd-mm-yyyy and mm/dd/yyyy is the separator.
Since you start with a string that appears to be in mm/yyyy format, the easiest solution would be to convert it to dd-mm-yyyy:
$date = '01/1990';
date('Y-m-d', strtotime('01-'.str_replace('/', '-', $date)));
And you also might want to check the DateTime class.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert date to timestamp in PHP?
I want to transfer some date to timestamp ,the date formart is %day %month %time. Like 25 nov 07:44, I tried some method below, but the two dates are different. need a help, thanks.
<?php
$date = '25 nov 07:44';
$time = explode(" ",$date);
$minute = explode(":",$time[2]);
$timestamp = mktime(''.$minute[0].' '.$minute[1].' 00 '.$time[1].' '.$time[0].' 2011')."<br />";// mktime(%hour, %minute, %second, %month, %day, %year);
echo $timestamp; //1322202671
echo date("Y-m-j H:i:s", $timestamp); //2011-11-25 07:31:11
?>
A few problems:
You are feeding mktime a string instead of a comma separated list
$time[1] is a string and mktime needs a number for the month
You might want to give it a try with strtotime or otherwise add a translation table to go from your month strings to a number.
In recent versions of php (5.3+), you can use DateTime::createFromFormat().
Try something like
$format = 'd M H:i';
$date = DateTime::createFromFormat($format, $timestamp);
echo $date->format('Y-m-j H:i:s');
Personally, I find this class based approach a little cleaner.
PHP Docs:
DateTime::createFromFormat
date() for info on format strings
I am having the input box for getting the value in mm-dd-yyyy format.So When I get the date I try to covert it into YYYY-MM-DD format.But It doesnt work.
For Example :
<?
$birthdate="08-13-2000";
$date=date('Y-m-d',strtotime($birthdate));
echo $date;
?>.
Output is 1970-01-01.
But If I gave 13-08-2000 I got 2000-08-13.I dont want to split.Because in my application I used in manyplaces like this.But I think the strtotime convert it into unix timestamp format even whatever the value.That's why I am try to do like.What's wrong I am understanding or doing?Thanks in advance.
strtotime() parses dash-separated dates as dd-mm-yyyy. You'll need to input birthdate as "08/13/2000". str_replace should do the job for that if you can't change the expected seperator for the input.
credit for the separator differences to sam at frontiermedia dot net dot au from php.net
Edit: Have some sample code for if you need to do the replace:
$birthdate = '08-13-2000';
$birthdate = str_replace('-','/',$birthdate);
$date = date('Y-m-d',strtotime($birthdate));
echo $date;
Otherwise it'd just be
$birthdate = '08/13/2000';
... snip ...
Try this:
$date = explode("-", "08-13-2000"); // input MM-DD-YYYY
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]); // to time
$date = date('Y-m-d', $time); // transform to Y-m-d
echo $date; // output YYYY-MM-DD
$date[0] is the month
$date[1] is the day
$date[2] is the year
And if you use it in manyplaces, make a function():
function transformDate($input)
{
$date = explode("-", $input);
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]);
$date = date('Y-m-d', $time);
return $date;
}
echo transformDate("08-13-2000");