Converting date shows 31-Dec-69 [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I want to convert date into d-M-y format and it seems that i am doing something wrong. Kindly help me to correct it.
<?php
$date = '30/04/2017';
echo date('d-M-y', strtotime($date));
?>
My Output:
31-Dec-69
By I want output as 30-Apr-17

Use DateTime objects when you're working with dates and times. You can use DateTime::createFromFormat() to parse the date string and then the DateTime::format() to format it the way you want:
<?php
$str = '30/04/2017';
$date = DateTime::createFromFormat('d/m/Y', $str);
echo $date->format('d-M-Y');
?>
Working Demo

Use DateTime::createFromFormat()
$date = DateTime::createFromFormat('d/m/Y', '30/04/2017');
echo $date->format('d-M-Y');

Try this:
<?php
$date = '30/04/2017';
$newDate = str_replace('/', '-', $date);
echo date('d-M-y', strtotime($newDate));
?>
// Output: 30-Apr-17
Working Example

<?php
$date = '30-04-2017';
echo date('d-M-y', strtotime($date));
?>
or use
<?php
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('d-M-y', strtotime($date));
?>

Related

Timestamp convert of US date format [duplicate]

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

How to convert String to Date in php? [duplicate]

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

Converting incoming Date in PHP

How can I convert a date from a format like this 13/Février/2016 to 2016-02-13 (Y-m-d) ?
You will first need to transform the month to English or a numeric value.
then all of the above answers will work
$month_name=array("","Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août",
"Septembre","Octobre","Novembre","Décembre");
$dateinput = '13/'.array_search("Février",$month_name).'/2016';
$date = date("Y-m-d",strtotime(str_replace('/', '-', $dateinput)));
echo $date;
You need to reformat the datestring using the DateTime class, see below for the answer.
$myDateTime = DateTime::createFromFormat('DD/m/YY', $dateString);
$newDateString = $myDateTime->format('Y-m-d');
This may help you:)
<?php
$changedate = str_replace("/","-","13/February/2016");
echo date("Y-m-d",strtotime($changedate));
?>
You can use strftime() and setlocale() for other language.
$date = "13/Février/2016";
setlocale(LC_TIME, 'fr_FR');
echo strftime("%Y-%b-%d", strtotime($date));
$myDateTime = DateTime::createFromFormat('DD/m/YY', $dateString);
$newDateString = $myDateTime->format('Y-m-d');

How do I convert a date from a datepicker input to a certain format in PHP? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I am new to PHP. I have a date string from a date-picker input field. But when I try to convert date string it into a date, I get an unexpected date.
Sample error:
If input ($datestamp) contains 24/01/2016, then it becomes $date = '70/01/01 [error]
Here is the PHP code:
<?php
$datestring = (isset($_POST['datetimepicker1'])) ? $_POST['datetimepicker1'] : "not";
$date = date("y/d/m", strtotime($datestring));
echo $datestring;
echo var_dump($date);
?>
updates:
here is my datepicker code
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker({format:"DD/MM/YYYY", useCurrent: false });
});
</script>
Please help me fix this. Is this a format problem? I need to insert the date into an Oracle database.
Please replace date function with below code. I have used explode to extract year, month and date separately, pass it to strtotime function.
<?php
$datestring = (isset($_POST['datetimepicker1'])) ? $_POST['datetimepicker1'] : "not";
$date_arr = explode('/',$datestring);
$date = date("Y/m/d", strtotime($date_arr[2].$date_arr[1].$date_arr[0]));
echo $datestring;
echo var_dump($date);
?>
According to jQuery, the datepicker default format is the following:
mm/dd/yy.
So, you can try this (in order to convert the $datestring to the y/m/d format):
<?php
$datestring = '24/01/2016';
list($day, $month, $year) = explode('/', $datestring);
$date = DateTime::createFromFormat('Ymd', $year . $month . $day);
echo $date->format('y/m/d');
?>
You can also change the datepicker:
var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
Use this code
<?php
$datestring = (isset($_POST['datetimepicker1'])) ? $_POST['datetimepicker1'] : "not";
$datestring = str_replace('/', '-', $datestring);
$date = date("y/d/m", strtotime($datestring));
echo $datestring;
echo var_dump($date);
?>
Just replace('/') to ('-') before passing to strtotime function. Sometime ('/') not work.

php date not converting properly

I have this date in php: 31/01/2013
I'm trying to convert it using the strtotime function like so
date("Y-m-d", strtotime(31/01/2013));
but it keeps displaying as 1970-01-01. Any know why this is?
you should include it inside a string, not a continuous series of dividing numbers
date("Y-m-d", strtotime("31/01/2013"));
This will work
$date = str_replace("/", "-", "31/01/2013");
echo date("Y-m-d", strtotime($date));
Try this
$date = "31/01/2013";
$date = date("Y-m-d", strtotime($date));
Hope it will help
Try this
$date = "01/08/2013";
echo date('Y-m-d', $date);

Categories