How to get Custom date format from timestamp php [duplicate] - php

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.

Related

PHP output timestamp correctly [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I am currently outputting a timestamp from my database (code below) but it's in the wrong format.
<p>{$row->last_updated}</p>
It is currently outputting 2018-10-23 13:36:40
The time is ok but the date isn't. This is what I want:
23-10-2018 13:36:40
What do I need to do with this code in order to display it like that?
You can use date()
<p><?php echo date('d-m-Y H:i:s',strtotime($row->last_updated));?></p>
You can change date format using PHP date function like this :
date('d-m-Y H:i:s', strtotime($row->last_updated))

Php date format for mysql [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm having a date format like this:
2010-09-21T00:00:00+03:00
how can i convert it to 'Y-m-d H:i:s' format?
I've tried following code but I don't think it's working:
$date = new DateTime('2010-09-21T20:00:00+03:00');
$status_date = $date->format('Y-m-d H:i:s');
It prints time like this: 2010-09-21 20:00:00
It prints exactly want you want ! So what's wrong ?
You can also use Carbon and to like this :
Carbon::parse('2010-09-21T00:00:00+03:00')->toDateTimeString();
UPDATE
Be careful, you said you want midnight but you wrote 8pm in your php !

Month and Year date conversion [duplicate]

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

Change date 2013-08-09 => 09-Aug-2013 [duplicate]

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

Get a timestamp from a day,year, and month [duplicate]

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.

Categories