-1

This is not an duplicate question. I am using a php date function for format a date.

my date value is : close_date:08/25/2017.

This is php function : $close_date = date('Y-m-d', strtotime($close_date));

print the close_date , result is : 2017-08-26.

my timezone is : -5:00 and server is on UTC , so at time of format date , date is convert into the utc date ?

so date is just format the date or convert date as per set timezone ?

Help me to solve this problem.

I don't want to convert my date to any timezone date, i just want to change format of the date.

My Input is: 08/25/2017

Expected output is: 2017-08-25

5
  • 3
    Possible duplicate of How to convert UTC datetime to another timezone? Commented Aug 26, 2017 at 7:18
  • No, this is not a duplicate. Commented Aug 26, 2017 at 7:19
  • 1
    Please share your input and expected output. Commented Aug 26, 2017 at 7:21
  • try this $close_date="08/25/2017"; echo $close_date = date('Y-m-d', strtotime($close_date)); Commented Aug 26, 2017 at 7:23
  • Need to set your default timezone within your file Commented Aug 26, 2017 at 7:24

2 Answers 2

2

You may want to use DateTime to create date object from desired format and convert it to format you need.

DateTime::createFromFormat("m/d/Y","08/25/2017")->format("Y-m-d");
Sign up to request clarification or add additional context in comments.

3 Comments

you sure , this date not depend on any timezone?
@chiragsatapara It just parses a time string according to a specified format.
You said you just need to convert dates, in case of timezone manipulation you may use DateTime class as well - DateTime::setTimeZone();
1

In case, you don't want to use PHP date function they try to use DateTime::createFromFormat OR write some manual code stuff to explode $close_date and use it a way you want.

1st Method:

Use DateTime::createFromFormat. This will help you to parse a time string according to a specified format.

$close_date = DateTime::createFromFormat("m/d/Y", $close_date)->format("Y-m-d");

2nd Method:

Use some manual code:

$close_date = "08/25/2017";
$date_vars = preg_split("#/#", $close_date); 
print_r($date_vars);

then use $date_vars to convert in required format like following code:

$close_date = $date_vars[2]."-".$date_vars[0]."-".$date_vars[1];

P.S: You can also use PHP explode() function to explode the date values.

2 Comments

Right! =) Just to parse a time string according to a specified format.
If you may want any timezone manipulation then use setTimezone at the end. Check it here: createFromFormat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.