This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I want to change format of date by 2013-08-09 to 09-Aug-2013, I tried this:
echo $date=date("Y-m-d");
//output 2013-08-09
echo date("d-M-Y",mktime(0-0-0,$date));
but this code showing 10-Aug-2013
I dont know why this showing date 10 instead of 09.
Answer would be highly appreciated , thanks in advance
following is easier:
echo date("d-M-Y",strtotime($date));
at mktime you have to subtract one day
You aren't passing in the parameters to mktime properly. mktime requires 6 integer parameters where as you have 1 integer and 1 string here.
Try using a DateTime object instead:
$date = new DateTime();
echo $date->format('d-M-Y');
Related
This question already has answers here:
Accessing dates in PHP beyond 2038
(5 answers)
Closed 5 years ago.
When I try to convert very high dates, such as 2045-01-01, I get another date:
date("Ymd", strtotime("2045-02-15"));
I obtain a wrong date
19700101
but when
date("Ymd", strtotime("2017-02-15"));
I have the good date
20170215
I don't understand why? Someone just explain to me what's going on?
that's a unixtimestamp problem (=> https://de.wikipedia.org/wiki/Unixzeit#/media/File:Year_2038_problem.gif), better:
date_parse("2006-12-12 10:00:00");
date_parse_from_format ( 'Ymd' , "2017-02-15" );
or
$date = DateTime::createFromFormat('Ymd', "2017-02-15");
echo $date->format('Y-m-d');
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a time stamp like this code below.
$time=time();
Please Explain how can i make a custom date from this.
See the documentation here: http://php.net/manual/en/function.date.php
string date ( string $format [, int $timestamp = time() ] )
As an example:
echo date('Y-m-d', $time);
Please use php builtin date function like this.
$time=time();
$custom_date=date('Y-m-d',$time);
echo $custom_date;
i hope you understand.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a Credit card expiry date with month and year only coming in form of a string say 08/17, How can I change this string in a format so that I can pass it to Authorize.net
$creditCard->setExpirationDate( "2017-08");
I have tried to use strtotime() but it is giving me the current year
echo $date = date('Y-m', strtotime($ccInfo['exp_date']));
You should use date_create_from_format instead of strtotime to build your date:
echo date_create_from_format('m/y', '08/17')->format('Y-m');
The function creates a \DateTime object, so you can call format to get the format you want.
You can also write like this
$eventDate = DateTime::createFromFormat('m/y', '08/17');
echo date_format($eventDate, 'Y-m');
This question already has answers here:
How to convert date to timestamp in PHP?
(22 answers)
Closed 9 years ago.
I have the following string: 2013-03-22
I need to convert that to a UNIX Timestamp.
How do I do this using PHP?
Use the strtotime function of PHP.
Like
$unixstamp = strtotime("2013-03-22");
With DateTime class
$dateTime = DateTime::createFromFormat('Y-m-d', '2013-03-22');
$dateTime->setTime(0, 0, 0);
echo $dateTime->getTimestamp();
Simple:
echo $time = strtotime("2013-03-22");
Using
strtotime("2013-03-22")
It will convert the date sting into timestamp value.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert this string to datetime
I have to convert my string value into datetime.Is there any php function which can convert varchar value into datatime.my value are coming like this:
$a= "2013-Jan-12-(Sat) 03:30 PM";
and i want to convert it simple datetime. Like: 2013-1-12 15:30:45
$Date = new DateTime("13-Jan-12-(Sat) 03:30 PM")
should do the trick. if thats not working, look at strtotime.
May be the sat is the problem try to change that or add that at first, you may get it.