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.
Related
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 6 years ago.
I trying to get a date in the d/m/Y H:i format. Here's my code
$dateStr = "28/07/2016 10:00";
echo date('d/m/Y H:i', strtotime($dateStr));
Here's the ouput:
01/01/1970 01:00
I trying to convert to other formats, but the result still the same date. I know this kind of obvious but still can't understand why i'm getting that date as result.
You can use DateTime class and it's createFromFormat method to parse the string to date in required format.
Like this,
$date = DateTime::createFromFormat('d/m/Y H:i', "28/07/2016 10:00");
$date = $date->format('Y-m-d H:i:s');
echo $date;
Use Following code
$dateStr = "28/07/2016 10:00";
$dateStr =str_replace("/","-",$dateStr);
echo date('d/m/Y H:i', strtotime($dateStr));
This code achieves what you want to do:
$dateString = '28/07/2016 10:00';
$date = \DateTime::createFromFormat('d/m/Y H:i', $dateString);
echo($date->format('d/m/Y H:i'));
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Hello i am trying to convert this date.
01. 06. 2015
Into mysql format, so i can insert it into database.
I tried this code, and other ombination with date and date format function.
`$mysqldate = date( 'Y-m-d H:i:s', strtotime($datefrom));`
but i get result
1.1.1970
Is it possible to do this with some date function, or i must use regex for resolving this problem. Maybe dateformat function?
Use this.. works fine
$datefrom = "01. 06. 2015";
$datefrom = str_replace( ". ", "-", $datefrom);
$mysqldate = date( 'Y-m-d H:i:s', strtotime($datefrom));
you need to clean your dateformat
<?php
$date = '01. 06. 2015';
$date = str_replace(' ', '', $date); // replace whitespaces
$mysqldate = date( 'Y-m-d H:i:s', strtotime($date));
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)
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a PHP variable '$date', and it's value is: y/m/d h:i:s format.
e.g: $date = "2014-11-10 17:25:00";
I want to change this format to m/d/y h:i:s format, so that I'm using PHP code below. It's working fine but is there any PHP function to do it in one line of code ?
$date = "2014-11-10 17:25:00";
$explode = explode(" ", $date);
$part1 = $explode[0];
$part2 = $explode[1];
$explode = explode("-", $part1);
$year = $explode[0];
$month = $explode[1];
$day = $explode[2];
$created_date = "$month-$day-$year $part2";
You can use date() & strtotime() functions. Example:
$date = "2014-11-10 17:25:00";
echo date('m-d-Y H:i:s', strtotime($date));
Output:
11-10-2014 17:25:00