kisham08
kisham08

Reputation: 43

time() gives the same value for two different dates

I have the following codes here:

$start_date = date_parse('10-17-2017');
$end_date = date_parse('10-30-2017');

$start_time = time($start_date);
$now = time();
$end_time = time($end_date);

var_dump($start_time,$end_time, $now);

The output is this:

int(1508383949) int(1508383949) int(1508383949)

Why are they giving the same values?

I also tried using strtotime,

 $start_time = strtotime($start_date);
 $now = time();
 $end_time = strtotime($end_date);

And had this as the result:

bool(false) bool(false) int(1508384298)

Upvotes: 0

Views: 250

Answers (2)

nithinTa
nithinTa

Reputation: 1642

The time function does not take any arguments. So the following wont work.

time($start_date);

with strtotime you need to have your date in either m/d/y or d-m-y format. Please check the hyphen and slash.

From the official docs of strtotime

Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

Upvotes: 1

SamBrishes
SamBrishes

Reputation: 127

Top of the mornin’ to you,

You get the same values, because

  1. time doesn't accept any parameters
  2. you use date_parse / strtotime not correctly

strtotime allows - next to many others - this 2 formats for dates: m/d/y and d-m-y. So you need to change your values from 10-17-2017 to 10/17/2017 or to 2017-10-17.

My Solution:

<?php

    $start_time = strtotime('10/17/2017');
    $end_time   = strtotime('10/30/2017');
    $now        = time();

    var_dump($start_time, $end_time, $now);

Output:

int(1508212800) int(1509336000) int(1508385505) 

You find more informations on PHP.net, just look for the strtotime and the time functions. :3

Sincerely,
Sam

Upvotes: 0

Related Questions