1

I've a date in this format: Tuesday 11th October, 2016 stored in a php varaible $pastdate

I want to convert this to: 2016-10-11 23:59:59

I'm using this line of code:

$newDate = date("Y-m-d", strtotime($pastDate)).' 23:59:59';

However, the output of $newDate is: 2017-10-17 23:59:59

Can anyone explain why this is happening and how I should better format my code? Thanks in advance.

Jack
  • 65
  • 2
  • 9
  • Are you sure there isn't something else going on? This works just fine for me. – Mureinik Jun 06 '17 at 07:04
  • `$newDate = DateTime::createFromFormat('l dS F, Y', $pastDate, new DateTimezone('UTC'))->setTime(23, 59, 59)->format('Y-m-d H:i:s');` – Mark Baker Jun 06 '17 at 07:13

2 Answers2

0

use the date function and pass the parameters for hour, minute and second to get the complete date and time

$newDate = date("Y-m-d H:i:s", strtotime($pastDate));
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
0

Here we are changing date from one format to another. Date format reference

Try this code snippet here

<?php

ini_set('display_errors', 1);
$date=date_create_from_format("l dS F, Y", 'Tuesday 11th October, 2016');
$newDate= $date->format("Y-m-d");
echo $newDate." 23:59:59";

Output: 2016-10-11 23:59:59

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • this has worked perfectly for my needs. I can see where I was going wrong now - thanks. Other solutions below look great too - thanks for your time :-) – Jack Jun 06 '17 at 07:22