chirag satapara
chirag satapara

Reputation: 1947

date format issue using php

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

Upvotes: -1

Views: 172

Answers (2)

Satish Saini
Satish Saini

Reputation: 2968

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.

Upvotes: 1

user1375602
user1375602

Reputation:

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

Upvotes: 2

Related Questions