I am trying to display the date that I send by $_GET, my url format is for example http://127.0.0.1/index.php?date=01/08/2018.
The result is 08-01-2018 it change the order of the day and the month why ?
$orginal_date= $_GET['date'];
$date = date("d-m-Y", strtotime($original_date));
echo $date;
It automatically parses the date with format m/d/Y (american format). You could use the DateTime class to specify your format:
$date = DateTime::createFromFormat('d/m/Y', $_GET['date']);
echo $date->format('d-m-Y');
Documentation:
DateTime::createFromFormat
DateTime::format
list($month, $day, $year) = explode("/", $_GET["date"]);
$date = sprintf("%d-%02d-%02d", $year, $month, $day);
This looks like you may be assuming how php will use strtotime and convert to a timestamp. By default it thinks that 01/08/2018 is Jan 8 2018. There is a method date_create_from_format that you can tell it the format you are expecting and then you can format that to however you want it to look like.
$orig = $_GET['date'];
$date = date_create_from_format('d/m/Y', $orig);
echo date_format($date, 'd-m-Y');
//result is 01-08-2018
Try This
list($month, $day, $year) = explode("/", $_GET["date"]);
echo date('d-m-Y',mktime(0, 0, 0, $month,$day, $year));
Related
I have a date string in the format DD-MM-YYYY .I need to get the day,month and year as separate strings.Is there a inbuilt function in php or should i use the string manipulation functions and write a function on my own.
EDIT:
What i have tried
$getdate = DateTime::createFromFormat("d-m-Y", $date);
echo $date->format("d");
error:
Call to a member function format() on a non-object in...
<?php
$year = date("Y", strtotime($yourDate));
$month = date("m", strtotime($yourDate));
$day = date("d", strtotime($yourDate));
?>
Edit
You can also this approach
$date = explode("-", $yourDate);
$day = $date[0];
$month = $date[1];
$year = $date[2];
This should work for you:
(With this you have day, month and year as separate variables)
Here I just explode() the string and save them with a list()
$date = "15-03-2015";
list($day, $month, $year) = explode("-", $date);
You bet! You can use the inbuilt DateTime class to parse a date, based on a format that you expect:
$format = 'd-m-Y';
$date = DateTime::createFromFormat($format, '15-02-2009');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
Source: http://php.net/manual/en/datetime.createfromformat.php
1.2 and need to convert a date from dd/mm/yyyy to yyyy-mm-dd
For example if the date is in format 07/08/2014, it should appear as 2014-08-07
How can this be done? I know strtotime returns unix timestamp but it doesn't seem to work with dates with Slashes (/) in it. SInce I'm using 5.1, a lot of DateTime functions are not supported in it.
Please help.
Use DateTime class, strtotime function would create issue when date less then 1901 with PHP 5.3.0
Try this way
$date = DateTime::createFromFormat('d/m/Y', "07/08/2014");
$new_date_format = $date->format('Y-m-d');
Need to pass a correct format with -(date string separation with dash) in date() try
$d = str_replace('/', '-','07/08/2014');
echo date('Y-m-d', strtotime($d)); //2014-08-07
with DateTime
$objDateTime = new DateTime($d);
echo $objDateTime->format('Y-m-d'); //2014-08-07
You can do it by date('Y-m-d',strtotime($date))
Where $date is in any format that you want to convert to YYYY-MM-DD format.
By using date() function yuo can try this
echo date('Y-d-m',strtotime('07/08/2014'));
Check the documentation for more
Method : 1 demo
$date1 = "07/08/2014";
$arr = explode("/", $date1);
$date2 = $arr[2]."-".$arr[1]."-".$arr[0];
echo $date2;
Method : 2 demo
$date1 = "07/08/2014";
list($day, $month, $year) = explode("/", $date1);
$date2 = $year."-".$month."-".$day;
echo $date2;
Method 3 : with strtotime Demo
$date1 = "07/08/2014";
$date1 = str_replace("/", "-", $date1);
$date2 = date('Y-m-d', strtotime($date1));
echo $date2;
I have this piece of code which gets me a field from the database:
$end_date=$row1['end_date'];
If i print it it gives me something like: 25-09-2012
What i need is to get the month value, the year and date.
something like:
$month=09;
$day=25;
$year=2012;
How can i do that?
thanks!
Using DateTime:
$date = new DateTime($row1['end_date']);
$year = $date -> format('Y');
$month = $date -> format('m');
$day = $date -> format('d');
If your timestamps are all like the one provided, keep it simple:
list($day, $month, $year) = explode('-', $row1['end_date']);
In your case, you can use the explode function like this :
// store a string containing "25-09-2012"
$end_date = $row1['end_date'];
// split "25-09-2012" into an array of three elements
$thedate = explode("-", $end_date);
// retrieve the values
$month = $thedate[0]; // 25
$day = $thedate[1]; // 09
$year = $thedate[2]; // 2012
try
[month('end_date')]
[day('end_date')]
[year('end_date')]
Or use explode and use - as the delimiter
Take a peak at this helpful tutorial describing various formatting methods and useful date functions in PHP:
Date/Time Functions
Date Formats
A. You can use DateTime
$date = DateTime::createFromFormat('d-m-Y',$row1['end_date']);
$month = $date->format("m");
$day = $date->format("d");
$year = $date->format("Y");
B. Using strtotime
$date = strtotime($row1['end_date']);
$month = date("m", $date);
$day = date("d", $date);
$year = date("Y", $date);
C. You can just sscanf scan through the string
$date = sscanf($row1['end_date'], "%d-%d-%d");
$month = $date[0] ;
$day = $date[1] ;
$year = $date[2] ;
D. Another method is using list & explode
list($day, $month, $year) = explode('-', $row1['end_date']);
Do it on a single line and format it however you would like. (Dec, December, 12) and so on with date().
list($month, $day, $year) = explode('-', date('m-d-Y', strtotime($row1['end_date'])));
$values = getdate(strtotime($row1['end_date']));
echo $values['mon']; //month
echo $values['mday']; //day
echo $values['year']; //year
I have a problem in this code:
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
function dateImplodeFunction($year, $month, $day){
$array = array($year, $month, $day);
$date = date('Y-m-d', strtotime( implode("-", $array)));
return $date;
}
based on the code above I'm going to create a function where there are 3 inputs month, day and a year. When I input those 3 these variables will passed to this function, combine those 3 variables and use the implode function to create a format based on what date you specified. for instance let's say if I input 10/01/1989 it will echo the display 10/01/1989.
also I need to use date function together with strtotime function (refer to the code above) for database, setting my date field into date data type.
The problem here is if I input 10-01-1989, it returns/dipslays the value of 01-01-1970 why??
I figured it out that there is a conflict between strtotime and implode function due to test. I've search through google but it find none. I hope you can help me. Thanks in advance.
sorry for the bad english =P
I would just use the Date Time class.
$month = '07';
$day = '26';
$year = '2012';
$timezone = new DateTimeZone('America/New_York');
$date = new DateTime( "{$year}-{$month}-{$day}", $timezone );
print $date->format('Y-m-d H:i:s');
# Output: 2012-07-26 00:00:00
http://codepad.org/VxwDHPeU
You should use mktime :
function dateImplodeFunction($year, $month, $day)
{
return date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
}
Documentation of mktime here
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");